is there any alternative to savefig?
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
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
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
participants (3)
-
Dag Sverre Seljebotn
-
Pauli Virtanen
-
Robert Kiwanuka