[Numpy-discussion] NumPy-Discussion Digest, Vol 40, Issue 70

Robert Kiwanuka robert.kiwanuka@gmail....
Thu Jan 28 09:43:29 CST 2010


[snip]

>
> Message: 2
> Date: Thu, 28 Jan 2010 12:50:03 +0000
> From: Robert Kiwanuka <robert.kiwanuka@gmail.com>
> Subject: [Numpy-discussion] is there any alternative to savefig?
> To: numpy-discussion@scipy.org
> Message-ID:
>        <b1e2a9a21001280450t6e9c9ee1n351ea3dcc86f80ef@mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi all,
>
> I wonder if anyone knows any alternative function in pylab (or
> otherwise) that could be used to save an image. My problem is as
> follows:
>
> ---------------
> from pylab import *
> ...
>
> figure(1)
> fig1 = gca()
> figure(2)
> fig2 = gca()
> figure(3)
> fig3 = gca()
>
> for i,data_file in enumerate(data_file_list):
>  time,x, y,x2, y2 = read_csv_file_4(open
> (data_file),elements=num_of_
> elements)
>  fig1.plot(-x,-y,color=colours[i],label=labellist[i])
>  fig2.plot(time,-y,color=colours[i],label=labellist[i])
>  fig3.plot(time,-x,color=colours[i],label=labellist[i])
>
> fig1.legend(loc='best')
> fig1.set_title("y1 - x1")
> fig1.set_ylabel("y1")
> fig1.set_xlabel("x1")
> #savefig("y1-x1.png")
>
> fig2.legend(loc='best')
> fig2.set_title("y1 - time")
> fig2.set_ylabel("y1")
> fig2.set_xlabel("time[s]")
> #savefig("y1-time.png")
>
> fig3.legend(loc='best')
> fig3.set_title("x1 - time")
> fig3.set_ylabel("x1")
> fig3.set_xlabel("time[s]")
> #savefig("x1-time.png")
> show()
> ---------------------------
>
> In the above code, I read multiple data files and plot three separate
> figures. Now I would like to save each of the figures to a file as the
> commented savefig satements suggest. The trouble is that if I
> uncomment all those savefig statements, I get three saved images all
> containing the plot belonging to figure(3), which was the last figure
> declared.
>
> I understand this to be happening because savefig will save the
> "current" figure, which in this case happens to be the last one
> declared.
>
> If I could do something like fig1.savefig("y1-x1.png") or savefig("y1-
> x1.png").fig1, this would solve the problem but I'm not aware of any
> such methods  or modules to enable this. This is thus a flaw in the
> general design/implementation of the savefig function, but is there an
> alternative function to enable me achieve what I need? Is there
> perhaps a possible tweak to savefig to make it do the same?
>
> Thanks in advance,
>
> Robert
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100128/057ec695/attachment-0001.html
>
> ------------------------------
>
> Message: 3
> Date: Thu, 28 Jan 2010 14:57:08 +0200
> From: Pauli Virtanen <pav@iki.fi>
> Subject: Re: [Numpy-discussion] is there any alternative to savefig?
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Message-ID: <1264683428.16723.18.camel@talisman>
> Content-Type: text/plain; charset="UTF-8"
>
> to, 2010-01-28 kello 12:50 +0000, Robert Kiwanuka kirjoitti:
> [clip]
> > If I could do something like fig1.savefig("y1-x1.png") or savefig("y1-
> > x1.png").fig1, this would solve the problem but I'm not aware of any
> > such methods  or modules to enable this. This is thus a flaw in the
> > general design/implementation of the savefig function, but is there an
> > alternative function to enable me achieve what I need? Is there
> > perhaps a possible tweak to savefig to make it do the same?
>
> Well, you almost had the answer there: use
>
>        fig1fig = figure(1)
>        ...
>
>        fig1fig.savefig("y1-x1.png")
>        fig2fig.savefig("y1-time.png")
>        fig3fig.savefig("x1-time.png")
>
> to save the respective figures.
>
> There is a separate mailing list for Matplotlib-specific questions:
> http://sourceforge.net/mail/?group_id=80706
>
> --
> Pauli Virtanen
>
>
>
> ------------------------------
>
> Message: 4
> Date: Thu, 28 Jan 2010 14:00:25 +0100
> From: Dag Sverre Seljebotn <dagss@student.matnat.uio.no>
> Subject: Re: [Numpy-discussion] is there any alternative to savefig?
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Message-ID: <4B618A69.5030701@student.matnat.uio.no>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Robert Kiwanuka wrote:
> > Hi all,
> >
> > I wonder if anyone knows any alternative function in pylab (or
> > otherwise) that could be used to save an image. My problem is as
> > follows:
> >
> > ---------------
> > from pylab import *
> > ...
> >
> > figure(1)
> > fig1 = gca()
> > figure(2)
> > fig2 = gca()
> > figure(3)
> > fig3 = gca()
> You should not use the pylab interface for stuff like this. This is much
> easier if you get rid of the notion of "current plot".
>
> from matplotlib import pyplot as plt
>
> fig1 = plt.figure()
> ax1 = fig1.add_subplot(111)
>
> ax1.plot_something...
>
> fig1.savefig(...)
>
> Etc., see matplotlib docs.
>
> Dag Sverre
>
> [snip]


Many thanks to both of you! It is all fine now:
The real problem was,  having defined e.g.

fig1 = plt.figure(1).gca()

"fig1.savefig" would not work! Instead, I should have used
"plt.figure(1).savefig" where the "gca()" part is trimmed off!

Regards,

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100128/b2a46e87/attachment.html 


More information about the NumPy-Discussion mailing list