2

The following code produces two pie charts side by side. Is there any way to add a title for each of the two pie charts - preferably above the chart itself?

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]

fig = plt.figure()

ax1 = fig.add_axes([0, 0, .5, .5], aspect=1)
ax1.pie(fracs, labels=labels, radius = 1.2)
ax2 = fig.add_axes([.5, .0, .5, .5], aspect=1)
ax2.pie(fracs, labels=labels, radius = 1.2)
plt.show()

enter image description here

BogdanC
  • 1,316
  • 3
  • 16
  • 36

1 Answers1

5

Billy Ferguson's answer raised an error for me. I suspect that this is related to the fact that ax1.title on its own returns a text object, not the string itself, and expects the same. If that is the case for you as well, you might try:

import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]

fig = plt.figure()

ax1 = fig.add_axes([0, 0, .5, .5], aspect=1)
ax1.pie(fracs, labels=labels, radius = 1.2)
ax2 = fig.add_axes([.5, .0, .5, .5], aspect=1)
ax2.pie(fracs, labels=labels, radius = 1.2)
ax1.set_title('Title for ax1')
ax2.set_title('Title for ax2')
plt.show()
saintsfan342000
  • 1,724
  • 1
  • 13
  • 16