Friday, March 05, 2010

[Py] Save the figure before show it

This might be a simple and trivial tips, but I didn't know it till today. I occasionally got a blank figure saved by matplotlib's savefig function and had no idea about that strange result. Then I decided to test it with simple Python code.

My test code:
--------------------------------------------
1 from pylab import *
2

3
y = []
4
for x in range(0,10):

5
y.append(x*x)

6

7
plot(range(0,10), y)

8
savefig('fig_saved.png')

9
show()

--------------------------------------------

After running the above code, you should get the foll0wing figure saved by using savefig() function.But if you exchange line 8 and line 9, you get nothing but a totally blank figure.

No comments:

Post a Comment