Python 多个饼图:图例不显示文本

Python multiple pie charts: legends does not display text

我正在显示 5 个带有不同图例的饼图。我从五个不同的数据帧(每个数据帧有两列:Legend_text 和 Legend_value)中检索数据并存储在一个列表中。 五个数据框是:

# df1

Legend_text,Legend_value
Weather Winds,80.27728
Vegetation - Touching / Burning,36.47659
Equipment Deterioration,30.34069

# df2

Legend_text,Legend_value
Equipment Deterioration,26.71819
Weather Snow,14.85159
Unknown,14.64567
Vegetation - Touching / Burning,13.4837
Weather Winds,12.74284
Foreign Interference Vehicles,5.20347

# df3

Legend_text,Legend_value
Equipment Deterioration,22.90246
Unknown,22.89272
Vegetation - Touching / Burning,13.81959
Foreign Interference Vehicles,10.59179
Weather Winds,9.22017
Foreign Interference Excavations,6.27836
Vegetation - Windblown Debris,5.12722
Foreign Interference Objects,4.58217
Weather Rain,4.54669

# df4

Legend_text,Legend_value
Equipment Deterioration,40.09754
Weather Winds,32.06516
Weather Snow,24.96378
Vegetation - Touching / Burning,15.98534
Foreign Interference Vehicles,13.8329
Unknown,13.58333

# df5

Legend_text,Legend_value
Equipment Deterioration,38.91777
Unknown,22.34813
Weather Winds,21.65532
Vegetation - Touching / Burning,16.63129
Foreign Interference Animals,14.42144
Lightning,12.26881
Foreign Interference Vehicles,8.34043

# read dataframes in with
df = pd.read_clipboard(sep=',')

存储数据帧值后的列表如下所示:

 [                  Legend_text     Legend_value    
0                    Weather Winds  80.27728  
1  Vegetation - Touching / Burning  36.47659  
2          Equipment Deterioration  30.34069,
            Legend_text              Legend_value    
0          Equipment Deterioration  26.71819  
1                     Weather Snow  14.85159  
2                          Unknown  14.64567  
3  Vegetation - Touching / Burning  13.48370  
4                    Weather Winds  12.74284  
5    Foreign Interference Vehicles   5.20347,
         Legend_text              Legend_value    
0           Equipment Deterioration  22.90246  
1                           Unknown  22.89272  
2   Vegetation - Touching / Burning  13.81959  
3     Foreign Interference Vehicles  10.59179  
4                     Weather Winds   9.22017  
5  Foreign Interference Excavations   6.27836  
6     Vegetation - Windblown Debris   5.12722  
7      Foreign Interference Objects   4.58217  
8                      Weather Rain   4.54669,
         Legend_text              Legend_value    
0          Equipment Deterioration  40.09754  
1                    Weather Winds  32.06516  
2                     Weather Snow  24.96378  
3  Vegetation - Touching / Burning  15.98534  
4    Foreign Interference Vehicles  13.83290  
5                          Unknown  13.58333,
        Legend_text              Legend_value    
0          Equipment Deterioration  38.91777  
1                          Unknown  22.34813  
2                    Weather Winds  21.65532  
3  Vegetation - Touching / Burning  16.63129  
4     Foreign Interference Animals  14.42144  
5                        Lightning  12.26881  
6    Foreign Interference Vehicles   8.34043 ]

我使用以下代码生成多个饼图:

fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5)
ax1.pie(temp_list[0]['Legend_value'],autopct='%1.1f%%')
ax2.pie(temp_list[1]['Legend_value'],autopct='%1.1f%%')
ax3.pie(temp_list[2]['Legend_value'],autopct='%1.1f%%')
ax4.pie(temp_list[3]['Legend_value'],autopct='%1.1f%%')
ax5.pie(temp_list[4]['Legend_value'],autopct='%1.1f%%')

first_legend = ax1.legend(temp_list[0]['Legend_text'], loc = 3)
second_legend = ax2.legend(temp_list[1]['Legend_text'], loc = 3)
third_legend = ax3.legend(temp_list[2]['Legend_text'], loc = 3)
fourth_legend = ax4.legend(temp_list[3]['Legend_text'], loc = 3)
fifth_legend = ax5.legend(temp_list[4]['Legend_text'], loc = 3)

plt.show()

上面的代码我遇到了两个问题: a) 图例不显示文字 b) 图例位于左下角,我想将图例移动到饼图的正下方(饼图不重叠)

谁能指导我哪里出错了?

plt.legend中使用bbox_to_anchor(0, 0, ,0, 0)移动您的标签。 first_legend = ax1.legend(temp_list[0]['Legend_text'], loc = 3)temp_list’Legend Text.

之间使用逗号
  • 必须指定 .locbbox_to_anchor 参数
fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5)

ax1.pie(df1['Legend_value'], autopct='%1.1f%%')
ax2.pie(df2['Legend_value'], autopct='%1.1f%%')
ax3.pie(df3['Legend_value'], autopct='%1.1f%%')
ax4.pie(df4['Legend_value'], autopct='%1.1f%%')
ax5.pie(df5['Legend_value'], autopct='%1.1f%%')

legend_1 = ax1.legend(df1['Legend_text'], bbox_to_anchor= (0.5, -0.04), loc='upper center')
legend_2 = ax2.legend(df2['Legend_text'], bbox_to_anchor= (0.5, -0.04), loc='upper center')
legend_3 = ax3.legend(df3['Legend_text'], bbox_to_anchor= (0.5, -0.04), loc='upper center')
legend_4 = ax4.legend(df4['Legend_text'], bbox_to_anchor= (0.5, -0.04), loc='upper center')
legend_5 = ax5.legend(df5['Legend_text'], bbox_to_anchor= (0.5, -0.04), loc='upper center')

plt.show()

参考文献: