使用Pandas在Matplotlib中设置Yaxis

dar*_*dog 23 matplotlib pandas ipython-notebook

使用Pandas在I-Python Notebook中绘图,我有几个图,因为Matplotlib决定Y轴,它们设置不同,我们需要使用相同的范围比较这些数据.我已经尝试了几种变体:(我假设我需要对每个情节应用限制..但因为我不能得到一个工作......从Matplotlib doc看来我似乎需要设置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()
Run Code Online (Sandbox Code Playgroud)

Rut*_*ies 36

Pandas plot()返回轴,你可以用它来设置ylim.

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

ax1.set_ylim(100000,500000)
ax2.set_ylim(100000,500000)
etc...
Run Code Online (Sandbox Code Playgroud)

您也可以将轴传递给Pandas图,因此可以在同一轴上绘制它,如:

ax1 = df2250.plot()
df2260.plot(ax=ax1)
etc...
Run Code Online (Sandbox Code Playgroud)

如果你想要很多不同的图,那么在正手和一个图中定义轴可能是一个可以让你获得最大控制权的解决方案:

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...
Run Code Online (Sandbox Code Playgroud)


ske*_*r88 27

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

df.plot(ylim=(0,200))

有关详细信息,请参阅pandas文档.