使用 Pandas 在 Matplotlib 中设置 Yaxis

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17787366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:10:50  来源:igfitidea点击:

Setting Yaxis in Matplotlib using Pandas

matplotlibpandasipython-notebook

提问by dartdog

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.

使用 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()

回答by Rutger Kassies

Pandas plot() returns the axes, you can use it to set the ylim on it.

Pandas plot() 返回轴,您可以使用它在其上设置 ylim。

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

ax1.set_ylim(100000,500000)
ax2.set_ylim(100000,500000)
etc...

You can also pass an axes to Pandas plot, so plotting it in the same axes can be done like:

您还可以将轴传递给 Pandas 图,因此可以在相同的轴上绘制它,如下所示:

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

回答by skeller88

I'm guessing this was a feature added after this answer was accepted in 2013; DataFrame.plot() now exposes a ylimparameter that sets the y axis limits:

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

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

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

See pandas documentationfor details.

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