使用 pandas 在Matplotlib中设置Yaxis [英] Setting Yaxis in Matplotlib using Pandas
问题描述
使用Pandas在I-Python Notebook中进行绘图,我有多个绘图,并且由于Matplotlib决定了Y轴,所以将它们设置为不同的值,我们需要使用相同范围比较该数据. 我已经尝试了几种变体:(我想我需要将限制应用于每个图..但是由于我无法正常工作...从Matplotlib文档看来,我需要设置ylim,但是可以
Using Pandas to plot in I-Python Notebook, I have several plots and because Matplotlib decides the Y axis it is setting them differently and we need to compare that data using the same range. I have tried several variants on: (I assume I'll need to apply the limits to each plot.. but since I can't get one working... From the Matplotlib doc it seems that I need to set ylim, but can't figure the syntax to do so.
df2250.plot(); plt.ylim((100000,500000)) <<<< if I insert the ; I get int not callable and if I leave it out I get invalid syntax. anyhow, neither is right...
df2260.plot()
df5.plot()
推荐答案
Pandas plot()返回轴,您可以使用它在其上设置ylim.
Pandas plot() returns the axes, you can use it to set the ylim on it.
ax1 = df2250.plot()
ax2 = df2260.plot()
ax3 = df5.plot()
ax1.set_ylim(100000,500000)
ax2.set_ylim(100000,500000)
etc...
您还可以将轴传递给Pandas图,因此可以在相同的轴上进行绘制,如下所示:
You can also pass an axes to Pandas plot, so plotting it in the same axes can be done like:
ax1 = df2250.plot()
df2260.plot(ax=ax1)
etc...
如果您需要许多不同的图,则正手在一个图形上定义轴可能是一种可以最大程度地控制您的解决方案:
If you want a lot of different plots, defining the axes on forehand and within one figure might be a solution that gives you most control:
fig, axs = plt.subplots(1,3,figsize=(10,4), subplot_kw={'ylim': (100000,500000)})
df2260.plot(ax=axs[0])
df2260.plot(ax=axs[1])
etc...
这篇关于使用 pandas 在Matplotlib中设置Yaxis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!