Multiple pie charts using matplotlib

I am trying to display two charts at the same time using matplotlib.

But I need to close one chart, then I can see another chart. In any case, you need to display both graphs and more graphs at the same time.

Here is my code

num_pass=np.size(data[0::,1].astype(np.float))
num_survive=np.sum(data[0::,1].astype(np.float))
prop=num_survive/num_pass
num_dead=num_pass-num_survive
#print num_dead


labels='Dead','Survived'
sizes=[num_dead,num_survive]
colors=['darkorange','green']
mp.axis('equal')
mp.title('Titanic Survival Chart')
mp.pie(sizes, explode=(0.02,0), labels=labels,colors=colors,autopct='%1.1f%%', shadow=True, startangle=90)
mp.show()

women_only_stats = data[0::,4] == "female" 
men_only_stats = data[0::,4] != "female" 

# Using the index from above we select the females and males separately
women_onboard = data[women_only_stats,1].astype(np.float)     
men_onboard = data[men_only_stats,1].astype(np.float)

labels='Men','Women'
sizes=[np.sum(women_onboard),np.sum(men_onboard)]
colors=['purple','red']
mp.axis('equal')
mp.title('People on board')
mp.pie(sizes, explode=(0.01,0), labels=labels,colors=colors,autopct='%1.1f%%', shadow=True, startangle=90)
mp.show()

How can I show both graphs at the same time?

+4
source share
3 answers

There are several ways to do this, and the easiest way is to use multiple numbers of digits. Just let us know matplotlibthat you are working on individual numbers and then showing them at the same time:

import matplotlib.pyplot as plt

plt.figure(0)
# Create first chart here.

plt.figure(1)
# Create second chart here.

plt.show() #show all figures
+8
source

:

from matplotlib import pyplot as plt
import numpy as np

data1 = np.array([0.9, 0.1])
data2 = np.array([0.6, 0.4])

# create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# plot each pie chart in a separate subplot
ax1.pie(data1)
ax2.pie(data2)

plt.show()
+6

Alternatively, you can place multiple pies on the same drawing using subtitles / multiple axes:

mp.subplot(211)
mp.pie(..)
mp.subplot(212)
mp.pie(...)
mp.show()
+1
source

All Articles