'轴线收集和额外线'

2024-05-23 19:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在绘图中添加了一个线条集合

https://stackoverflow.com/a/24593105/1127601

我怎样才能在带有辅助y轴的绘图中添加第二条直线?我想显示的不仅仅是绘图中的线集合。 数据是基于pandas数据帧的,如果我用我想绘制的列来调用plot,那么plot根本没有显示任何内容。在

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

行集合加附加行 Line Collection plus additional line

谢谢!在

编辑:

移动

^{pr2}$

在代码块的顶部,渲染图不是完全无用的,但我还是看不到我想看到的。如果可能的话,一个比另一条线更好的解决方案是将条形图与线集合在一起。在


Tags: 数据绘图dfplotnp绘制pltpoints
1条回答
网友
1楼 · 发布于 2024-05-23 19:21:04

这些附加行可以:

#df['another']=np.random.random(some_number)
ax2=ax.twinx()
ax2.bar(left=x, height=df.another, width=0.02)

绘制时间序列时,不要将pandas.plotmatplotlib混合在一起,因为它们对日期时间值的处理方式不同。您可以通过在pandas.plot()调用前后调用plt.xlim()来验证它。在

enter image description here

相关问题 更多 >