使用Pandas在Matplotlib中设置Yaxis

使用Pandas在Matplotlib中设置Yaxis,matplotlib,pandas,ipython-notebook,Matplotlib,Pandas,Ipython Notebook,使用Pandas在I-Python笔记本中绘图,我有几个绘图,因为Matplotlib决定Y轴,所以设置它们的方式不同,我们需要使用相同的范围比较数据。 我已经尝试了几个变体:(我假设我需要对每个绘图应用限制..但是由于我无法使一个工作..从Matplotlib文档中,似乎我需要设置ylim,但无法找到这样做的语法 df2250.plot(); plt.ylim((100000,500000)) <<<< if I insert the ; I get int not c

使用Pandas在I-Python笔记本中绘图,我有几个绘图,因为Matplotlib决定Y轴,所以设置它们的方式不同,我们需要使用相同的范围比较数据。 我已经尝试了几个变体:(我假设我需要对每个绘图应用限制..但是由于我无法使一个工作..从Matplotlib文档中,似乎我需要设置ylim,但无法找到这样做的语法

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()
df2250.plot();plt.ylim((1000005000))Pandas plot()返回轴,您可以使用它设置轴上的ylim

ax1 = df2250.plot()
ax2 = df2260.plot()
ax3 = df5.plot()

ax1.set_ylim(100000,500000)
ax2.set_ylim(100000,500000)
etc...
您还可以将轴传递给Pandas plot,以便在相同的轴上进行打印,如下所示:

ax1 = df2250.plot()
df2260.plot(ax=ax1)
etc...
如果您需要许多不同的绘图,那么预先在一个图形中定义轴可能是提供最多控制的解决方案:

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...

我猜这是在2013年接受此答案后添加的功能;DataFrame.plot()现在公开了一个设置y轴限制的
ylim
参数:

df.plot(ylim=(0200))


有关详细信息,请参阅。

您还可以将
sharey=True
添加到
plt.子地块中。这样,即使在放大特定子地块时,y限制也将保持不变。