[英]matplotlib savefig() blank - show() isn't
I've browsed the "Questions that may already have your answer", and if they did... I didn't understand it. 我已经浏览了“可能已经有你答案的问题”,如果他们这样做了......我不明白。
I've tried to eliminate as much as I could and simplified my problem down to the following code (my apologies - I'm sure there's a more elegant way of getting a numpy array of all 40s, but this was what I came up with after banging my head....): 我试图尽可能地消除并将我的问题简化为以下代码(我的道歉 - 我确信有一种更优雅的方式来获得所有40个的numpy数组,但这就是我想出来的在敲我的头后....):
import numpy
import matplotlib.pyplot as plt
x=numpy.zeros((500,256))
x+=40
plt.title("Title")
plt.xlabel("Label")
plt.ylabel("Label")
plt.imshow(x,vmin=37.5,vmax=42.5, cmap='spectral')
#save the figure
filename="/xxx/yyy/matplotlibtest.jpg"
plt.savefig(filename)
plt.show()
plt.close()
plt.savefig() returns a very nice image with my labels, axes, and no figure (ie, the rectangle is blank) plt.savefig()返回一个非常漂亮的图像,包含我的标签,轴和没有图形(即矩形是空白的)
plt.show() shows me all of the above with a nice green rectangle where it belongs, in between my axes, as it ought to be. plt.show()向我展示了上面所有的一个漂亮的绿色矩形,它位于我的轴之间,就像它应该的那样。 I can manually save this figure to the same directory, and get the result I expect (ie, axes, labels, green rectangle).
我可以手动将此图保存到同一目录,并获得我期望的结果(即轴,标签,绿色矩形)。
I've tried this without the show() or by having the show() call come 2nd, as some questions & answers on here (and elsewhere) seemed to imply that the call order may be the problem. 我已经尝试了没有show()或show()调用第二,因为这里(和其他地方)的一些问题和答案似乎暗示调用顺序可能是问题。
I've saved out other figures (histograms of the same data) in this code as jpg's, so I know this computer can save as jpg. 我已经在这段代码中以jpg的形式保存了其他数据(相同数据的直方图),所以我知道这台计算机可以保存为jpg。
I'm not sure what I'm doing wrong with savefig - any suggestions? 我不确定我在savefig上做错了什么 - 有什么建议吗?
Thanks! 谢谢!
By adding: 通过增加:
import matplotlib
matplotlib.use('TkAgg')
in before my call to 在我打电话之前
import matplotlib.pyplot as plt
I'm able to get everything working happily. 我能够让一切都愉快地工作。
(I don't understand the backends yet, clearly!) (显然,我还不了解后端!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.