簡體   English   中英

多個餅圖的通用圖例

[英]Common legend for multiple pie chart plots

如何在圖表底部為兩個圖表設置共同圖例。 我嘗試使用 fig.legend 但不知道如何正確編寫它。 謝謝!!

import pandas as pd

# Intialise data to Dicts of series.
d = {'Oil' : pd.Series([1.0, 17.0, 0.3, 81.7],
                       index =['Others', 'Commercial', 'Industrial', 'Domestic']),
        'Coal' : pd.Series([3, 34.8,  38.6, 23.6],
                        index =['Others', 'Commercial', 'Industrial', 'Domestic'])}
  
# creates Dataframe.
df = pd.DataFrame(d)

fig, axes = plt.subplots(1,2, figsize=(15,4.5))

fig.suptitle('Customers Profile and Energy Sold per Sector', fontweight='bold', fontsize=20)
for ax, col in zip(axes, df.columns):
    ax.pie(df[col], wedgeprops=dict(linewidth = 3, edgecolor = 'white'), colors=['#EE8434','#C95D63', '#AE8799','#717EC3'],startangle=90, autopct='%.2f%%', pctdistance=1.2)
     
#fig.legend(ax, df.index, 'upper left')

答案在@henryecker 建議的鏈接中找到

for ax, col in zip(axes, df.columns):
    chart = ax.pie(df[col])
     
fig.legend(chart[0], df.index, 'lower center')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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