views:

531

answers:

3

I have written code that opens 16 figures at once. Currently they all open as separate graphs. I'd like them to open all on the same page. Not the same graph. I want 16 separate graphs on a single page/window. Also for some reason the format of the numbins and defaultreallimits doesn't hold past figure 1. Do I need to use the subplot command? I don't understand why I would have to but can't figure out what else I would do? Thanks again in advance!

import csv
import scipy.stats
import numpy
import matplotlib.pyplot as plt

for i in range(16):
    plt.figure(i)
    filename= easygui.fileopenbox(msg='Pdf distance 90m contour', title='select file', filetypes=['*.csv'], default='X:\\herring_schools\\')
    alt_file=open(filename)    
    a=[]
    for row in csv.DictReader(alt_file):
        a.append(row['Dist_90m(nmi)'])
    y= numpy.array(a, float)    
    relpdf=scipy.stats.relfreq(y, numbins=7, defaultreallimits=(-10,60))
    bins = numpy.arange(-10,60,10)
    print numpy.sum(relpdf[0])
    print bins
    patches=plt.bar(bins,relpdf[0], width=10, facecolor='black')
    titlename= easygui.enterbox(msg='write graph title', title='', default='', strip=True, image=None, root=None)
    plt.title(titlename)
    plt.ylabel('Probability Density Function')
    plt.xlabel('Distance from 90m Contour Line(nm)')
    plt.ylim([0,1])

plt.show()
+4  A: 

To answer your main question, you want to use the subplot command. I think changing plt.figure(i) to plt.subplot(4,4,i+1) should work.

las3rjock
+2  A: 

The answer from las3rjock is not correct--nor will it give you working code or help you to write working code.

First, calling 'subplot' doesn't give you multiple plots; 'subplot' is called to create a single plot as well as multiple plots. In addition, "changing plt.figure(i)" is not correct. plt.figure() creates a figure instance; this object's 'add_subplot' method is then called for every plot you create (whether just one or several on a page).

Below i've list the code to plot two plots on a page, one above the other. The formatting is done via the argument passed to 'add_subplot'. Notice it is (211) for the first plot and (212) for the second. These mean: "two rows; one column, plot number one," and "two rows, one column, plot number two."

So for instance, if instead you wanted four plots on a page, in a 2x2 matrix configuration, you would call the add_subplot method four times, passing in: (221), (222), (223), and (224), to create four plots on a page at 10, 2, 4, and 8 o'clock, respectively.

from matplotlib import pyplot as PLT
fig = PLT.figure()
ax1 = fig.add_subplot(211)
ax1.plot([(1, 2), (3, 4)], [(4, 3), (2, 3)])
ax2 = fig.add_subplot(212)
ax2.plot([(7, 2), (5, 3)], [(1, 6), (9, 5)])
PLT.show()
doug
A: 

This works also:

for i in range(19):
    plt.subplot(5,4,i+1) 

It plots 19 total graphs on one page. The format is 5 down and 4 across..

FS