[英]Reverse legend order pandas plot
我有以下代碼來顯示堆積條
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]
的圖例的順序,但是我收到了一個錯誤。
DataFrame.plot
采用一個legend
參數,可以是True / False /'reverse'。 你想要legend='reverse'
這是一個使用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
(我在上面的圖中使用了plt.style.use('ggplot')。)
另請參見matplotlib圖例指南 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.