Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib中使用两个轴打印三个类别_Python_Pandas_Matplotlib - Fatal编程技术网

Python 在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.

我使用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.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()