Python 在matplotlib中使用两个轴打印三个类别
我使用pandas和matplotlib的组合为几个类别绘制三个值。我希望一个地块有自己的轴,另外两个共享一个轴 关闭,但说明了我为什么需要双轴的问题:Python 在matplotlib中使用两个轴打印三个类别,python,pandas,matplotlib,Python,Pandas,Matplotlib,我使用pandas和matplotlib的组合为几个类别绘制三个值。我希望一个地块有自己的轴,另外两个共享一个轴 关闭,但说明了我为什么需要双轴的问题: pd.DataFrame([[1,2,3], [500,600,700], [500, 700, 650]], columns=['foo', 'bar','baz'], index=['a','b','c']).T.plot(kind='bar') 相反,我希望为a条设置第二个轴。我的尝试: smol = pd.
pd.DataFrame([[1,2,3], [500,600,700], [500, 700, 650]], columns=['foo', 'bar','baz'],
index=['a','b','c']).T.plot(kind='bar')
相反,我希望为a
条设置第二个轴。我的尝试:
smol = pd.DataFrame([[1,2,3], [500,600,700], [500, 700, 650]], columns=['foo', 'bar','baz'],
index=['a','b','c']).T
fig = plt.figure(figsize=(10,5)) # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
ax2 = ax.twinx() # Create another axes that shares the same x-axis as ax.
smol['a'].plot(kind='bar', color='red', ax=ax, width=0.3,
position=1, edgecolor='black')
smol['b'].plot(kind='bar', color='blue', ax=ax2, width=0.3,
position=0, edgecolor='black')
ax.set_ylabel('Small scale')
ax2.set_ylabel('Big scale')
plt.show()
不幸的是
smol['c'].plot(kind='bar', color='green', ax=ax2, width=0.3,
position=0, edgecolor='black')
产生:
我怎样才能让
b
和c
共享一个轴,但却像第一次尝试那样彼此相邻?我使用了次要关键字。代码也相当短
smol = pd.DataFrame([[1,2,3], [500,600,700], [500, 700, 650]], columns=['foo', 'bar','baz'],
index=['a','b','c']).T
ax = smol.plot(kind="bar", secondary_y=['b', 'c'])
ax.set_ylabel('Small scale')
ax.right_ax.set_ylabel('Big scale')
plt.show()