Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show two figures using matplotlib?

I have some troubles while drawing two figures at the same time, not shown in a single plot. But according to the documentation, I wrote the code and only the figure one shows. I think maybe I lost something important. Could anyone help me to figure out? Thanks. (The *tlist_first* used in the code is a list of data.)

plt.figure(1) plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer') plt.ylabel('Percentage of answered questions') plt.xlabel('Minutes elapsed after questions are posted')  plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min') plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour') plt.legend() plt.xlim(0,120) plt.ylim(0,1)  plt.show() plt.close() ### not working either with this line or without it  plt.figure(2) plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')  plt.ylabel('Percentage of answered questions') plt.xlabel('Minutes elapsed after questions are posted')  plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min') plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour') plt.legend(loc= 4) plt.xlim(0,2640) plt.ylim(0,1) plt.show() 
like image 431
AnneS Avatar asked Oct 12 '11 18:10

AnneS


People also ask

How do I display photos side by side in Matplotlib?

The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt. figure() and then add an axes object to the fig by calling add_subplot() method.

Can a Matplotlib figure have more than one set of axes?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2.


1 Answers

Alternatively to calling plt.show() at the end of the script, you can also control each figure separately doing:

f = plt.figure(1) plt.hist........ ............ f.show()  g = plt.figure(2) plt.hist(........ ................ g.show()  raw_input() 

In this case you must call raw_input to keep the figures alive. This way you can select dynamically which figures you want to show

Note: raw_input() was renamed to input() in Python 3

like image 186
joaquin Avatar answered Oct 20 '22 01:10

joaquin