plt.save saves a different image

I have the following code:

plt.figure()
plt.plot(Xu,Yu, 'g>',label='u')
plt.plot(Xv,Yv,'r^',label='v')
plt.plot(Xc,Yc,'kx',label='p')
plt.xlim(-0.2,1.2)
plt.ylim(-0.2,1.2)
plt.scatter(X,Y)
plt.grid()
#plt.legend()
plt.savefig('Grid.png')

Which makes the folowing Figure:

http://imgur.com/ZABzcDv

However when I open it in the folder where is saved, the legend dissapears.

Note: If I discomment the legend line, the result is the same, and the saved imaged looks like this:

http://imgur.com/xwNaCZZ

What could I do to save exactly, the image I create an not a changed version?

Upvotes: 1

Views: 243

Answers (1)

Ed Smith
Ed Smith

Reputation: 13196

What are the shapes of your velocity and pressure arrays? If you have them as 2D arrays of rows and columns then matplotlib will assume each row is a separate plot.As a work around you can ravel to expand them as a 1D array,

plt.plot(Xu.ravel(),Yu.ravel(), 'g>',label='u')
plt.plot(Xv.ravel(),Yv.ravel(),'r^',label='v')
plt.plot(Xc.ravel(),Yc.ravel(),'kx',label='p')

If this doesn't work, you can be even more explicit and just label the first three velocity and pressure locations and suppress the rest,

#Plot first element with labels
plt.plot(Xu.ravel()[0],Yu.ravel()[0], 'g>',label='u')
plt.plot(Xv.ravel()[0],Yv.ravel()[0],'r^',label='v')
plt.plot(Xc.ravel()[0],Yc.ravel()[0],'kx',label='p')

#plot remaining without Legend
plt.plot(Xu.ravel()[1:],Yu.ravel()[1:], 'g>',legend=False)
plt.plot(Xv.ravel()[1:],Yv.ravel()[1:],'r^',legend=False)
plt.plot(Xc.ravel()[1:],Yc.ravel()[1:],'kx',legend=False)

Upvotes: 1

Related Questions