Reputation: 1903
I have a line collection added to a plot
https://stackoverflow.com/a/24593105/1127601
How can I add a second line to such a plot with a secondary y-axis? I want to display more than just the line collection in the plot. The data is based on a pandas dataframe, if I just call plot with the column I'd like to have plotted the plot does show nothing at all.
fig, axes = plt.subplots()
x=mpd.date2num(df_resamplew1.index.to_pydatetime())
y=df_resamplew1.winds.values
c=df_resamplew1['temp'].values
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap=plt.get_cmap('copper'), norm=plt.Normalize(0, 10))
lc.set_array(c)
lc.set_linewidth(3)
#ax=plt.gca()
axes.add_collection(lc)
plt.xlim(min(x), max(x))
#plot a second line
df_resamplew1['diff_modulo'].plot(ax=axes, color='b', linewidth=2, label="Niederschlag mm/h")
axes.xaxis.set_major_locator(mpd.DayLocator())
axes.xaxis.set_major_formatter(mpd.DateFormatter('%Y-%m-%d'))
_=plt.setp(axes.xaxis.get_majorticklabels(), rotation=45 )
Line collection without trying to plot a second line
Line Collection plus additional line
Thanks!
EDIT:
Moving
df_resamplew1['diff_modulo'].plot(ax=axes, color='b', linewidth=2, label="Niederschlag mm/h")
to the top of the codeblock renders the plot not completey useless but I still don't see what I want to see. If it is possible an even better solution than an additional line would be a bar plot together with the line collection.
Upvotes: 1
Views: 2132
Reputation: 54400
These additional lines will do:
#df['another']=np.random.random(some_number)
ax2=ax.twinx()
ax2.bar(left=x, height=df.another, width=0.02)
Don't mix pandas.plot
and matplotlib
together when plotting time series, as they treat the date-time value differently. You can verify it by calling plt.xlim()
both before and after the pandas.plot()
call.
Upvotes: 3