【问题标题】:Common legend for multiple pie chart plots多个饼图的常用图例
【发布时间】:2021-10-01 10:05:07
【问题描述】:

如何在图表底部为两个图表设置一个通用图例。我尝试使用 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')

【问题讨论】:

标签: python pandas matplotlib jupyter-notebook pie-chart


【解决方案1】:

答案在@henryecker 建议的链接中找到

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2011-05-28
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多