Reputation: 2220
I am displaying 5 pie charts with separate legends. I retreieved the data from five different dataframes (each data frame has two columns: Legend_text and Legend_value) and stored in a list. Five dataframes are:
# 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=',')
The list after storing dataframes value looks like below:
[ 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 ]
I used the following code to produce the multiple pie charts:
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()
I am facing two issues with the above code: a) legend does not display the text b) legend is located at the left lower corner and I want to move legend exactly below to pie chart (without overlapping pie chart)
Could anyone please guide me where am I making the mistake?
Upvotes: 0
Views: 283
Reputation: 62403
.loc
and bbox_to_anchor
parameters must be specifiedfig, (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()
References:
Upvotes: 1
Reputation: 381
Use bbox_to_anchor(0, 0, ,0, 0)
in plt.legend
to move Your label.
first_legend = ax1.legend(temp_list[0]['Legend_text'], loc = 3)
Use comma between temp_list
and ’Legend Text
.
Upvotes: 0