kevin.desai
kevin.desai

Reputation: 250

Multiple pie charts using matplotlib

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

But I have to close one graph then only I can see the other graph. Is there anyway to display both the graphs or more number of 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 the graphs at the same time?

Upvotes: 6

Views: 39528

Answers (4)

Nijanth Anand
Nijanth Anand

Reputation: 281

Yes. This answer of User:Banana worked for me.

I had 4 graphs and all 4 popped up as individual pie charts when I ran the plt.show() so I believe you can use as many figure numbers as you want.

plt.figure(0)   # Create first chart here and specify all parameters below.
plt.figure(1)   # Create second chart here and specify all parameters below.
plt.figure(3)   # Create third chart here and specify all parameters below.
plt.figure(4)   # Create fourth chart here and specify all parameters below.
plt.show()      # show all figures.

Upvotes: 1

dwitvliet
dwitvliet

Reputation: 7671

There are several ways to do this, and the simplest is to use multiple figure numbers. Simply tell matplotlib that you are working on separate figures, and then show them simultaneously:

import matplotlib.pyplot as plt

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

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

plt.show() #show all figures

Upvotes: 14

mdurant
mdurant

Reputation: 28683

Alternatively, you can put multiple pies on the same figure using subplots/multiple axes:

mp.subplot(211)
mp.pie(..)
mp.subplot(212)
mp.pie(...)
mp.show()

Upvotes: 2

ali_m
ali_m

Reputation: 74172

In addition to Banana's answer, you could also plot them in different subplots within the same figure:

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()

Upvotes: 13

Related Questions