2
votes

I'm coding in Python and testing Matplotlib. I'm trying to add multiple legends because I have 2 pie charts. Using this http://matplotlib.org/users/legend_guide.html I got 2 multiple legends, but with the same colours. For every pie chart, I have 2 differents colors, so I want the same colours for their respectives legends.

Also, is it possible to put the legend closer to the border of the window? Because the legend is a long phrase and I don't want the second legend in the middle of the screen.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = 'Participaram', 'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = 'Só visualizam dúvidas alheias', 'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
plt.pie(sizes, explode=explode,  colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

plt.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

first_legend = plt.legend(labels,loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(labels2,loc = 4,ncol=1)

plt.axis('equal')
plt.show()

enter image description here

2

2 Answers

2
votes

Define two axes in the figure to draw every pie chart separately and for automatic adjust use tight_layout:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = [u'Só visualizam dúvidas alheias', u'Mandaram e visualizam']
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# Plot
ax1.pie(sizes, explode=explode,  colors=colors2, autopct='%1.1f%%', 
        shadow=True, startangle=90, center = (-2,0))

ax2.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', 
        shadow=True, startangle=45, center = (2,0))

first_legend = ax1.legend(labels, loc = 2)
second_legend = ax2.legend(labels2, loc = 2)

ax1.axis('equal')
ax2.axis('equal')
plt.tight_layout()
plt.show()

0
votes

Appart from the option to draw several subplots, you can still work with one single subplot, define two legends as in the example and provide the respective patches from each pie to the legend. You get the patches from the call to plt.pie() as

patches, legendlabels            = plt.pie(data, ... )
patches, legendlabels, pctlabels = plt.pie(data, ... , autopct='%1.1f%%' ...)

Where legendlabels are the legend labels, which are empty in this case, as you want to set them manually afterwards and pctlabels are the percentage values shown on the pie (if present).

The whole code would look like this:

import matplotlib.pyplot as plt

labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = u'Só visualizam dúvidas alheias', u'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
slices1, legendlabels1, pctlabels1 = plt.pie(sizes, explode=explode,
      colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

slices2, legendlabels2, pctlabels2 = plt.pie(sizes2, explode=explode2, 
      colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

# slices are the patches of the pie (cake pieces)
# legendlabels are the legend labels, which are empty in this case, 
#     as you want to set them manually afterwards
# pctlabels are the percentage values shown on the pie

# now we provide the patches to the legend, such that each legend knows which patches to draw
first_legend  = plt.legend(slices1, labels,  loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(slices2, labels2, loc = 4)

plt.axis('equal')
plt.show()

providing the following plot.

enter image description here