is there any alternative to savefig?

Alf P. Steinbach alfps at start.no
Thu Jan 28 10:47:33 EST 2010


* kiwanuka:
> On Jan 28, 12:29 pm, kiwanuka <robert.kiwan... at gmail.com> 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()
>>
>> 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
> 
> Problem solved.

That's nice, but would you mind telling the group the solution?

Assuming that you haven't (I don't see all messages posted to the e-mail list, 
only what's propagated to Usenet or originating there).


Cheers,

- Alf



More information about the Python-list mailing list