反向传奇顺序 pandas 情节 [英] Reverse legend order pandas plot

查看:66
本文介绍了反向传奇顺序 pandas 情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have following code to show stacked bar

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]的图例的顺序,但出现错误.

I want to reverse the order of legend I used handles=handles[::-1]but I got an error.

推荐答案

这是直接使用matplotlib作为图例的最小示例.

Here's a minimal example using matplotlib directly for the legend.

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

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

(I've used plt.style.use('ggplot') in the plot above.)

另请参见 matplotlib图例指南.

这篇关于反向传奇顺序 pandas 情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆