无法控制第二个Y轴的刻度测向图()

2024-05-16 23:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用secondary_y来绘制3个序列,其中2在左y轴上,1在右y轴上,但是我不清楚如何定义右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()根本不产生任何东西。我正在使用:

  • 斯派德2.3.5.2
  • python:3.4.3.final.0
  • python位:64
  • 操作系统:Windows
  • 操作系统版本:7
  • 熊猫:0.16.2
  • 数量:1.9.2
  • 压缩比:0.15.1
  • matplotlib:1.4.3

What I get for results

我发现这些链接很有用:

tcaswell, working directly with axes

matplotlib.axes documentation


Tags: dfindexplotmatplotlibwith绘制plt序列
3条回答

您需要在相应的ax上使用^{}。在

例如:

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

另外,检查您的代码,您似乎没有正确地指定您的iloc列。尝试:

^{pr2}$

你不用直接打电话就可以了双轴斧():

#Plot the first series on the LH y-axis
ax1 = df.plot('x_column','y1_column')

#Add the second series plot, and grab the RH axis
ax2 = df.plot('x_column','y2_column',ax=ax1)
ax2.set_ylim(0,10)

注:仅在熊猫19.2中测试

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10,3))
print (df)
fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(df.index,df.iloc[:,[0,2]])
ax2.plot(df.index, df.iloc[:,2])

plt.show()

相关问题 更多 >