6

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?

2
  • You should call mp.show() only at the end after creating all the plots. Commented Aug 8, 2014 at 14:07
  • @Banana did that too, it shows the second pie chart completely overlapping the first one. I can see the values of the first pie but not the actual chart. Commented Aug 8, 2014 at 14:09

4 Answers 4

14

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 
Sign up to request clarification or add additional context in comments.

1 Comment

It shows all figures separately
13

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

3 Comments

These look like oval charts, rather than circular... How to adjust their visualization?
@FC84 You can set the aspect ratio of the subplots to be equal, e.g. plt.subplots(1, 2, subplot_kw={'aspect':'equal'})
@ali_m, complication of OP: how to plot multiple pie charts centered at specified (x,y) coordinates in a single plot and with varying size?
2

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

Comments

1

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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.