简体   繁体   English

plt.save保存不同的图像

[英]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? 我可以做些什么来完全保存我创建的图像而不更改版本?

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, 如果将它们作为行和列的2D数组,则matplotlib会假设每一行都是单独的图。作为解决方法,您可以将它们扩展为一维数组,

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM