brucelim80
brucelim80

Reputation: 19

How do i put two pie charts side by side on matplotlib

I have the following code below

plt.figure(figsize=(5,5))
plt.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%')
plt.title('Gender Composition in 1960')
labels = male_and_female_2016['level_1']
values = male_and_female_2016['value']
#settings and configs for the pie charts
colors = ['#FF8F33','#33FFDC']
explode = (0.1, 0)
plt.figure(figsize=(5,5))
plt.pie(values,labels = labels,colors = colors,autopct = '%1.1f%%')
plt.title('Gender Composition in 2016')
plt.show() 

it show me like this enter image description here

How do i get it to be displayed side by side? thank you very much

Upvotes: 0

Views: 1534

Answers (1)

Roman Pavelka
Roman Pavelka

Reputation: 4171

Instead of using plt.figure twice, plt.pie and plt.title, use:

fig, axs = plt.subplots(1, 2, figsize=(6, 3))
# ...
axs[0].pie(values,labels = labels, colors = colors, autopct = '%1.1f%%')
axs[0].set_title('Gender Composition in 1960')
# ...
axs[1].pie(values,labels = labels, colors = colors, autopct = '%1.1f%%')
axs[1].set_title('Gender Composition in 2016')
# ...

See this example for inspiration: https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/categorical_variables.html#sphx-glr-gallery-lines-bars-and-markers-categorical-variables-py

Upvotes: 1

Related Questions