[snip]<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Message: 2<br>
Date: Thu, 28 Jan 2010 12:50:03 +0000<br>
From: Robert Kiwanuka <<a href="mailto:robert.kiwanuka@gmail.com">robert.kiwanuka@gmail.com</a>><br>
Subject: [Numpy-discussion] is there any alternative to savefig?<br>
To: <a href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a><br>
Message-ID:<br>
        <<a href="mailto:b1e2a9a21001280450t6e9c9ee1n351ea3dcc86f80ef@mail.gmail.com">b1e2a9a21001280450t6e9c9ee1n351ea3dcc86f80ef@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="iso-8859-1"<br>
<br>
Hi all,<br>
<br>
I wonder if anyone knows any alternative function in pylab (or<br>
otherwise) that could be used to save an image. My problem is as<br>
follows:<br>
<br>
---------------<br>
from pylab import *<br>
...<br>
<br>
figure(1)<br>
fig1 = gca()<br>
figure(2)<br>
fig2 = gca()<br>
figure(3)<br>
fig3 = gca()<br>
<br>
for i,data_file in enumerate(data_file_list):<br>
  time,x, y,x2, y2 = read_csv_file_4(open<br>
(data_file),elements=num_of_<br>
elements)<br>
  fig1.plot(-x,-y,color=colours[i],label=labellist[i])<br>
  fig2.plot(time,-y,color=colours[i],label=labellist[i])<br>
  fig3.plot(time,-x,color=colours[i],label=labellist[i])<br>
<br>
fig1.legend(loc='best')<br>
fig1.set_title("y1 - x1")<br>
fig1.set_ylabel("y1")<br>
fig1.set_xlabel("x1")<br>
#savefig("y1-x1.png")<br>
<br>
fig2.legend(loc='best')<br>
fig2.set_title("y1 - time")<br>
fig2.set_ylabel("y1")<br>
fig2.set_xlabel("time[s]")<br>
#savefig("y1-time.png")<br>
<br>
fig3.legend(loc='best')<br>
fig3.set_title("x1 - time")<br>
fig3.set_ylabel("x1")<br>
fig3.set_xlabel("time[s]")<br>
#savefig("x1-time.png")<br>
show()<br>
---------------------------<br>
<br>
In the above code, I read multiple data files and plot three separate<br>
figures. Now I would like to save each of the figures to a file as the<br>
commented savefig satements suggest. The trouble is that if I<br>
uncomment all those savefig statements, I get three saved images all<br>
containing the plot belonging to figure(3), which was the last figure<br>
declared.<br>
<br>
I understand this to be happening because savefig will save the<br>
"current" figure, which in this case happens to be the last one<br>
declared.<br>
<br>
If I could do something like fig1.savefig("y1-x1.png") or savefig("y1-<br>
x1.png").fig1, this would solve the problem but I'm not aware of any<br>
such methods  or modules to enable this. This is thus a flaw in the<br>
general design/implementation of the savefig function, but is there an<br>
alternative function to enable me achieve what I need? Is there<br>
perhaps a possible tweak to savefig to make it do the same?<br>
<br>
Thanks in advance,<br>
<br>
Robert<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <a href="http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100128/057ec695/attachment-0001.html" target="_blank">http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100128/057ec695/attachment-0001.html</a><br>


<br>
------------------------------<br>
<br>
Message: 3<br>
Date: Thu, 28 Jan 2010 14:57:08 +0200<br>
From: Pauli Virtanen <<a href="mailto:pav@iki.fi">pav@iki.fi</a>><br>
Subject: Re: [Numpy-discussion] is there any alternative to savefig?<br>
To: Discussion of Numerical Python <<a href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>><br>
Message-ID: <1264683428.16723.18.camel@talisman><br>
Content-Type: text/plain; charset="UTF-8"<br>
<br>
to, 2010-01-28 kello 12:50 +0000, Robert Kiwanuka kirjoitti:<br>
[clip]<br>
> If I could do something like fig1.savefig("y1-x1.png") or savefig("y1-<br>
> x1.png").fig1, this would solve the problem but I'm not aware of any<br>
> such methods  or modules to enable this. This is thus a flaw in the<br>
> general design/implementation of the savefig function, but is there an<br>
> alternative function to enable me achieve what I need? Is there<br>
> perhaps a possible tweak to savefig to make it do the same?<br>
<br>
Well, you almost had the answer there: use<br>
<br>
        fig1fig = figure(1)<br>
        ...<br>
<br>
        fig1fig.savefig("y1-x1.png")<br>
        fig2fig.savefig("y1-time.png")<br>
        fig3fig.savefig("x1-time.png")<br>
<br>
to save the respective figures.<br>
<br>
There is a separate mailing list for Matplotlib-specific questions:<br>
<a href="http://sourceforge.net/mail/?group_id=80706" target="_blank">http://sourceforge.net/mail/?group_id=80706</a><br>
<br>
--<br>
Pauli Virtanen<br>
<br></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
------------------------------<br>
<br>
Message: 4<br>
Date: Thu, 28 Jan 2010 14:00:25 +0100<br>
From: Dag Sverre Seljebotn <<a href="mailto:dagss@student.matnat.uio.no">dagss@student.matnat.uio.no</a>><br>
Subject: Re: [Numpy-discussion] is there any alternative to savefig?<br>
To: Discussion of Numerical Python <<a href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>><br>
Message-ID: <<a href="mailto:4B618A69.5030701@student.matnat.uio.no">4B618A69.5030701@student.matnat.uio.no</a>><br>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br>
<br>
Robert Kiwanuka wrote:<br>
> Hi all,<br>
><br>
> I wonder if anyone knows any alternative function in pylab (or<br>
> otherwise) that could be used to save an image. My problem is as<br>
> follows:<br>
><br>
> ---------------<br>
> from pylab import *<br>
> ...<br>
><br>
> figure(1)<br>
> fig1 = gca()<br>
> figure(2)<br>
> fig2 = gca()<br>
> figure(3)<br>
> fig3 = gca()<br>
You should not use the pylab interface for stuff like this. This is much<br>
easier if you get rid of the notion of "current plot".<br>
<br>
from matplotlib import pyplot as plt<br>
<br>
fig1 = plt.figure()<br>
ax1 = fig1.add_subplot(111)<br>
<br>
ax1.plot_something...<br>
<br>
fig1.savefig(...)<br>
<br>
Etc., see matplotlib docs.<br>
<br>
Dag Sverre<br>
<br>[snip]</blockquote><div><br>Many thanks to both of you! It is all fine now:<br>The real problem was,  having defined e.g.<br><br>fig1 = plt.figure(1).gca()<br> <br>"fig1.savefig" would not work! Instead, I should have used "plt.figure(1).savefig" where the "gca()" part is trimmed off!<br>

<br>Regards,<br><br>Robert<br> <br></div></div>