簡體   English   中英

反向傳奇順序熊貓情節

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM