 Hi all,

what is needed to save a figure when the size is given in pixels, i.e. 1024x772 ?
The default is 800x600 pixels.

from pylab import plot, savefig
from numpy import sin,linspace,pi
x = linspace(0,2*pi,200)
plot(x,sin(x))
savefig('test')

Nils

You can try creating a figure with the correct aspect ratio and then setting a corresponding dpi when you save:

···

On Sep 10, 2010, at 5:27 AM, Nils Wagner wrote:

Hi all,

what is needed to save a figure when the size is given in
pixels, i.e. 1024x772 ?
The default is 800x600 pixels.

from pylab import plot, savefig
from numpy import sin,linspace,pi
x = linspace(0,2*pi,200)
plot(x,sin(x))
savefig('test')

Nils

#---
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(10.24, 7.72))
x = np.linspace(0,2*np.pi,200)
plt.plot(x, np.sin(x))
plt.savefig('test', dpi=100)
#---

I had mixed results with this: I would occasionally get figures that were one pixel smaller than desired.

Best,
-Tony

Nils,

In matplotlib, you can specify a figure size in inches like so:

import matplotlib.pyplot as plt
from numpy import sin,linspace,pi

width_in = 10.0
width_px = 1024

height_px = 772
aspect = width_px / float(height_px)
height_in = width_in / aspect
dpi = width_px / width_in

x = linspace(0, 2*pi, 200)
fig = plt.figure(figsize=(width_in, height_in))
ax = fig.gca()

ax.plot(x, sin(x))

Then, when you save your figure, you can specify the “dots per inch” (dpi):

fig.savefig(‘test.png’, dpi=dpi)

I hope this helps!
Ben Root

···

On Fri, Sep 10, 2010 at 4:27 AM, Nils Wagner <nwagner@…1052…> wrote:

Hi all,

what is needed to save a figure when the size is given in

pixels, i.e. 1024x772 ?

The default is 800x600 pixels.

from pylab import plot, savefig

from numpy import sin,linspace,pi

x = linspace(0,2*pi,200)

plot(x,sin(x))

savefig(‘test’)

Nils

My understanding is that you set the figure size in *inches*,
and then by setting its ``dpi`` you determine the number of
pixels. (So "inches" are purely virtual, unless you match