Screenshot instructions:
Windows
Mac
Red Hat Linux
Ubuntu
Click URL instructions:
Rightclick on ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)
From: mogliii <mogliii@gm...>  20120625 17:12:20

Hi, In my script a variable number of graphs is generated. I want to place them in one column with arbitrary number of rows onto an A4 canvas (for pdf export). Unfortunately the figsize directive seems to have no effect. The figure is always 8x6 inch. Which code do I have to use in this case? My minimal code: ####################################### import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages import numpy as np x = np.arange(10) y = np.arange(10) fig_width_cm = 21 # A4 page fig_height_cm = 29.7 inches_per_cm = 1 / 2.58 # Convert cm to inches fig_width = fig_width_cm * inches_per_cm # width in inches fig_height = fig_height_cm * inches_per_cm # height in inches fig_size = [fig_width, fig_height] pdf = PdfPages('outfile.pdf') fig = plt.figure(figsize = fig_size) allplots = 3 # This is the variable number of subplots f, axarr = plt.subplots(allplots, 1) for plot in range(allplots): axarr[plot].plot(x+plot, y) pdf.savefig() pdf.close() 
From: Benjamin Root <ben.root@ou...>  20120625 17:31:19
Attachments:
Message as HTML

On Mon, Jun 25, 2012 at 1:12 PM, mogliii <mogliii@...> wrote: > Hi, > > In my script a variable number of graphs is generated. I want to place > them in one column with arbitrary number of rows onto an A4 canvas (for > pdf export). > > Unfortunately the figsize directive seems to have no effect. The figure > is always 8x6 inch. > > Which code do I have to use in this case? > > > My minimal code: > ####################################### > > import matplotlib.pyplot as plt > from matplotlib.backends.backend_pdf import PdfPages > import numpy as np > > x = np.arange(10) > y = np.arange(10) > > fig_width_cm = 21 # A4 page > fig_height_cm = 29.7 > inches_per_cm = 1 / 2.58 # Convert cm to inches > fig_width = fig_width_cm * inches_per_cm # width in inches > fig_height = fig_height_cm * inches_per_cm # height in inches > fig_size = [fig_width, fig_height] > > > pdf = PdfPages('outfile.pdf') > fig = plt.figure(figsize = fig_size) > allplots = 3 # This is the variable number of subplots > f, axarr = plt.subplots(allplots, 1) > > > for plot in range(allplots): > axarr[plot].plot(x+plot, y) > > pdf.savefig() > pdf.close() > > Your call to "plt.subplots" is creating a new figure object, which never gets the figsize parameter (only the old figure object has that set). Cheers! Ben Root 
From: Mogliii <mogliii@gm...>  20120625 18:09:22

On 25/06/12 18:30, Benjamin Root wrote: > > > > Your call to "plt.subplots" is creating a new figure object, which > never gets the figsize parameter (only the old figure object has that > set). > > Cheers! > Ben Root > Hi, indeed you are right. I added "f.set_size_inches(fig_size)" and it works Also I had a wrong conversion of inch to cm (2.58 before). Thank you for your help, Mogliii The final code: ################# import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages import numpy as np x = np.arange(10) y = np.arange(10) fig_width_cm = 21 # A4 page fig_height_cm = 29.7 inches_per_cm = 1 / 2.54 # Convert cm to inches fig_width = fig_width_cm * inches_per_cm # width in inches fig_height = fig_height_cm * inches_per_cm # height in inches fig_size = [fig_width, fig_height] pdf = PdfPages('outfile.pdf') allplots = 3 # This is the variable number of subplots f, axarr = plt.subplots(allplots, 1) f.set_size_inches(fig_size) for plot in range(allplots): axarr[plot].plot(x + plot, y) pdf.savefig() pdf.close() 
From: Mogliii <mogliii@gm...>  20120625 20:07:45

Even better: f, axarr = plt.subplots(allplots, 1, figsize = fig_size) Its always difficult to predict which **kwargs could/would be valid... 
Sign up for the SourceForge newsletter:
No, thanks