2016-07-18 5 views
0

Ich erstelle die 7 Plots mit dem folgenden Code. Ich hätte gerne eine gemeinsame Legende für alle 7 Parzellen, vorzugsweise in der oberen rechten Ecke. Für die grüne Region sollte die Legende "Daten senden" sein, für die rote Region sollte sie "Keine Daten senden" sein. Ich habe versucht mit figlegend aber konnte es nicht erreichen. Jede Hilfe wäre willkommen.Gemeinsame Legende für alle Kreisdiagramme mit Matplotlib

fig = plt.figure(figsize=(18,10), dpi=1600) 
ax1 = plt.subplot2grid((2,4),(0,0)) 
plt.pie(df_14,colors=("g","r")) 
plt.title('LOGS1') 
ax2 = plt.subplot2grid((2, 4), (0, 1)) 
plt.pie(df_24,colors=("g","r")) 
plt.title('LOGS2') 
ax3 = plt.subplot2grid((2, 4), (0, 2)) 
plt.pie(df_34,colors=("g","r")) 
plt.title('LOGS3') 
ax4 = plt.subplot2grid((2, 4), (0, 3)) 
plt.pie(df_44,colors=("g","r")) 
plt.title('LOGS4') 
ax5 = plt.subplot2grid((2, 4), (1, 0)) 
plt.pie(df_54,colors=("g","r")) 
plt.title('LOGS5') 
ax6 = plt.subplot2grid((2, 4), (1, 1)) 
plt.pie(df_64,colors=("g","r")) 
plt.title('LOGS6') 
ax7 = plt.subplot2grid((2, 4), (1, 2)) 
line7 = plt.pie(df_74,colors=("g","r")) 
plt.title('LOGS7') 

enter image description here

Antwort

2

Die Legende muss nur einmal anders aufgerufen werden, würden Sie 7 verschiedene Legenden bekommen zeigt. Ein Beispiel, von dem ich unten gezeigt habe. Beachten Sie, dass Sie in Ihren eigenen Daten in ax.pie() müssen ersetzt werden:

data1 = (10,90) # some data to be plotted 
data2 = (40,50) 
data3 = (70,30) 

labels = ['Sending Data', 'Not Sending Data'] #legend labels to be plotted 
colors = ['green', 'red'] 

fig = plt.figure(figsize=(16,8)) 

ax1 = plt.subplot2grid((2,4),(0,0)) 
ax1.pie(data1, colors=colors, startangle=90) 
plt.title('LOGS1') 

ax2 = plt.subplot2grid((2, 4), (0, 1)) 
ax2.pie(data2, colors=colors, startangle=90) 
plt.title('LOGS2') 

ax3 = plt.subplot2grid((2, 4), (0, 2)) 
ax3.pie(data3, colors=colors, startangle=90) 
plt.title('LOGS3') 

ax4 = plt.subplot2grid((2, 4), (0, 3)) 
ax4.pie(data1, colors=colors, startangle=90) 
plt.title('LOGS4') 

ax5 = plt.subplot2grid((2, 4), (1, 0)) 
ax5.pie(data2, colors=colors, startangle=90) 
plt.title('LOGS5') 

ax6 = plt.subplot2grid((2, 4), (1, 1)) 
ax6.pie(data3, colors=colors, startangle=90) 
plt.title('LOGS6') 

ax7 = plt.subplot2grid((2, 4), (1, 2)) 
patches, texts = ax7.pie(data1, colors=colors, startangle=90) #use this plot to show the legend 
plt.title('LOGS7') 
plt.legend(patches, labels, bbox_to_anchor=(2.3, 2), prop={'size':14}) #show the legend defined in labels 
#change values of 'bbox_to_anchor' to move the legend to the desired location 

plt.axis('equal') # Set aspect ratio to be equal so that pie is drawn as a circle. 
plt.tight_layout() 
plt.subplots_adjust(right=0.94) #adjust the spacing on right to see legend clearly 
plt.show() 

Dies erzeugt das zeigt dieses Diagramm:

enter image description here