反向传奇顺序熊猫情节

时间:2016-02-12 23:04:34

标签: python pandas matplotlib

我有以下代码来显示堆积条

handles = df.toPandas().set_index('x').T.plot(kind='bar', stacked=True, figsize=(11,11))
    plt.legend(loc='best', title="Line", fontsize = 'small', framealpha=0)
    plt.ylabel("'" + lineName + "'")
    plt.show()

我想要颠倒我使用handles=handles[::-1]的图例的顺序,但是我收到了错误。

2 个答案:

答案 0 :(得分:10)

groovy.lang.GroovyShell.evaluate(java.io.File)采用legend参数,可以是True / False /'reverse'。你想要legend='reverse'

答案 1 :(得分:7)

这是一个使用matplotlib直接用于图例的最小例子。

df = pd.DataFrame({'a': np.random.randn(10) + 1, 'b': np.random.randn(10),
                   'c': np.random.randn(10) - 1}, columns=['a', 'b', 'c'])
ax = df.plot(kind='bar', stacked=True)
handles, labels = ax.get_legend_handles_labels()
ax.legend(reversed(handles), reversed(labels), loc='upper left')  # reverse both handles and labels

bar chart

(我在上图中使用了plt.style.use(' ggplot')。)

另请参阅matplotlib legend guide