gpt4 book ai didi

python - 无法控制 df.plot() 上第二个 y 轴的比例

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:48 25 4
gpt4 key购买 nike

我正在尝试使用 secondary_y 绘制 3 个系列,其中 2 个在左侧 y 轴,1 个在右侧,但我不清楚如何定义右侧 y 轴刻度,因为我在左侧使用 ylim=() 进行操作。

我看过这篇文章:Interact directly with axes

...但是一旦我有了:

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() 根本不产生任何东西。我正在使用:

  • Spyder 2.3.5.2
  • python:3.4.3.final.0
  • python 位数:64
  • 操作系统:Windows
  • 操作系统版本:7
  • Pandas :0.16.2
  • numpy:1.9.2
  • scipy:0.15.1
  • matplotlib:1.4.3

What I get for results

我发现这些链接很有帮助:

tcaswell, working directly with axes

matplotlib.axes documentation

最佳答案

您需要使用set_ylim在适当的斧头上。

例如:

ax2 = ax1.twinx()
ax2.set_ylim(bottom=-10, top=10)

此外,检查您的代码,您似乎错误地指定了 iloc 列。尝试:

ax1.plot(df.index, df.iloc[:, :2])  # Columns 0 and 1.
ax2.plot(df.index, df.iloc[:, 2]) # Column 2.

关于python - 无法控制 df.plot() 上第二个 y 轴的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com