1

Example Plot I have a dataframe that includes time series of snow-water and temperature data. I am looking to create a time series plot of snow water, that shows two colors in the snow water line plot, 'blue' if the temperature is < = 273 deg K and 'red' if the temperature is > 273 deg K. I tried to follow the matplotib documentation (https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/multicolored_line.html) but have not been successful. Would appreciate some insights. Thank you!

My dataframe is as follows: Date (datetime64[ns]); Snow-water (float64) and Temp (float64)

from matplotlib.collections import LineCollection

Date                  Snowwater  Temperature
2014-01-01 01:00:00   5           240
2014-01-01 02:00:00   10          270
2014-01-01 03:00:00   11          273
2014-01-01 04:00:00   15          279
2014-01-01 05:00:00   20          300
2014-01-01 06:00:00   25          310

I am looking for output something like in the example plot linked above but with snow-water values in the y-axis (line color blue or red depending on the temperature) and datetime on the x-axis

1
  • Please post the relevant code that you've written and explain why it didn't work. Commented Sep 10, 2020 at 4:18

2 Answers 2

1

This did the trick although there might be better way of doing it:

colors=['blue' if x < 273 else 'red' for x in df['AIR_T[K]']]
x = mpd.date2num(df['Date'])
y = df['SWE_St'].values
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, colors=colors)

fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.xaxis.set_major_locator(mpd.MonthLocator())
ax.xaxis.set_major_locator(ticker.MultipleLocator(200))
ax.xaxis.set_major_formatter(mpd.DateFormatter('%Y-%m-%d:%H:%M:%S'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation=70)
plt.show()

result plot

Sign up to request clarification or add additional context in comments.

Comments

0

I created the data manually, but LineCollection is a This is an object that contains multiple lines, the first argument being a list of lines.

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

xs = [0, 1, 2, 3, 4, 5]
ys = [-2, -1, 0, 1, 5, 10]
lines = [[(x1, y1), (x2, y2)] for x1, y1, x2, y2 in zip(xs, ys, xs[1:], ys[1:])]

colors = ['r', 'r', 'b', 'b', 'b']
lc = LineCollection(lines, colors=colors)

fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
plt.show()

enter image description here

3 Comments

Thanks, I got some ideas from your post although it didn't exactly answer my question...
What's the difference? The data has been corrected after the answer and the expected graphs have been added.
The data wasn't corrected, I just changed the unit of temperature from deg C to deg K, the question was still the same...the difference is that i needed time on the x-axis and y-axis depended on an another variable...i managed to get something after going through many other codes including yours... i added the graph i got so that others with similar question can benefit...i do sincerely appreciate your contribution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.