Python 具有多个y轴的pandas/matplotlib绘图
我有一个数据帧,如下所示:Python 具有多个y轴的pandas/matplotlib绘图,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个数据帧,如下所示: Rate per 100,000 population Gambling EXP per Adult Local Government Area City of Banyule 7587.7 555.188876
Rate per 100,000 population Gambling EXP per Adult
Local Government Area
City of Banyule 7587.7 555.188876
City of Bayside 5189.9 171.282451
City of Boroondara 4877.0 141.675636
City of Brimbank 9739.0 904.959407
City of Casey 7790.6 561.086313
我曾多次尝试使用两个y轴进行绘图,这两个y轴对应于右边的两列,最左边的一列是x轴。但到目前为止,我只设法为这两个轴都获得了一个轴。我曾试图模仿这里的解决方案:但我一直失败。我也看过其他stackoverflow Q&S,但到目前为止还不太清楚。如果有人能帮我解决这个问题就太好了。干杯。你真的应该包含你尝试过的代码,这样人们才能真正为你指明正确的方向。尽管如此,我猜想您已经错过了pandas将打开一个新的图形和新的axis对象,除非您指定一个先前存在的轴来进行打印
import pandas as pd
import matplotlib.pyplot as plt
# separate data file
dat = pd.read_csv('dat.csv', index_col='Local Government Area')
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
# the ax keyword sets the axis that the data frame plots to
dat.plot(ax=ax1, y='Rate per 100 000 population', legend=False)
dat.plot(ax=ax2, y='Gambling EXP per Adult', legend=False, color='g')
ax1.set_ylabel('Rate per 100,000 population')
ax2.set_ylabel('Gambling EXP per Adult')
plt.show()
你需要更多地使用它来获得一个好看的情节,但这应该让你开始。你真的应该包括你尝试过的代码片段,这样人们才能真正为你指明正确的方向。尽管如此,我猜想您已经错过了pandas将打开一个新的图形和新的axis对象,除非您指定一个先前存在的轴来进行打印
import pandas as pd
import matplotlib.pyplot as plt
# separate data file
dat = pd.read_csv('dat.csv', index_col='Local Government Area')
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
# the ax keyword sets the axis that the data frame plots to
dat.plot(ax=ax1, y='Rate per 100 000 population', legend=False)
dat.plot(ax=ax2, y='Gambling EXP per Adult', legend=False, color='g')
ax1.set_ylabel('Rate per 100,000 population')
ax2.set_ylabel('Gambling EXP per Adult')
plt.show()
你需要更多的努力才能得到一个好看的情节,但这应该让你开始