无法在df.plot()上控制第二个y轴的比例 [英] Unable to control scale of second y-axis on df.plot()
本文介绍了无法在df.plot()上控制第二个y轴的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用secondary_y
绘制3系列,其中左侧y轴上有2个,右侧是1个,但是我不清楚如何定义右侧y轴比例,就像我在左侧那样ylim=()
.
I am trying to plot 3 series with 2 on the left y-axis and 1 on the right using secondary_y
, but it’s not clear to me how to define the right y-axis scale as I did on the left with ylim=()
.
我看过这篇文章:直接与轴
…但是一旦我拥有:
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randn(10,3))
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(df.index,df.iloc[:,[0,2]])
ax2.plot(df.index, df.iloc[:,2])
plt.show()
完全不产生任何东西.我正在使用:
plt.show()
doesn't produce anything at all. I am using:
- Spyder 2.3.5.2
- python:3.4.3.final.0
- python位:64
- 操作系统:Windows
- 操作系统版本:7
- 熊猫:0.16.2
- numpy:1.9.2
- scipy:0.15.1
- matplotlib:1.4.3
我发现这些链接很有帮助:
I found these links helpful:
推荐答案
您需要使用 set_ylim
在适当的斧头上.
You need to use set_ylim
on the appropriate ax.
例如:
ax2 = ax1.twinx()
ax2.set_ylim(bottom=-10, top=10)
此外,查看您的代码,您似乎在错误地指定了iloc
列.试试:
Also, reviewing your code, it appears that you are specifying your iloc
columns incorrectly. Try:
ax1.plot(df.index, df.iloc[:, :2]) # Columns 0 and 1.
ax2.plot(df.index, df.iloc[:, 2]) # Column 2.
这篇关于无法在df.plot()上控制第二个y轴的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文