Matplotlib (pyplot) savefig 输出空白图像 [英] Matplotlib (pyplot) savefig outputs blank image

查看:56
本文介绍了Matplotlib (pyplot) savefig 输出空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存使用 matplotlib 绘制的图;但是,图像保存为空白.

I am trying to save plots I make using matplotlib; however, the images are saving blank.

这是我的代码:

plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)

plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)

plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')

if T0 is not None:
    plt.subplot(123)
    plt.imshow(T0, cmap=mpl.cm.bone)

    #plt.subplot(124)
    #Autozoom

#else:
    #plt.subplot(124)
    #Autozoom

plt.show()
plt.draw()
plt.savefig('tessstttyyy.png', dpi=100)

而 tessstttyyy.png 是空白的(也尝试使用 .jpg)

And tessstttyyy.png is blank (also tried with .jpg)

推荐答案

首先,当T0不是None时会发生什么?我会测试它,然后我会调整我传递给 plt.subplot() 的值;也许尝试值 131、132 和 133,或者取决于 T0 是否存在的值.

First, what happens when T0 is not None? I would test that, then I would adjust the values I pass to plt.subplot(); maybe try values 131, 132, and 133, or values that depend whether or not T0 exists.

其次,在调用 plt.show() 之后,会创建一个新图形.要解决这个问题,您可以

Second, after plt.show() is called, a new figure is created. To deal with this, you can

  1. 在调用plt.show()

在你show()之前通过调用plt.gcf()获取当前图形"来保存图形,然后你可以调用savefig() 随时在这个 Figure 对象上.

Save the figure before you show() by calling plt.gcf() for "get current figure", then you can call savefig() on this Figure object at any time.

例如:

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('tessstttyyy.png', dpi=100)

在您的代码中,tesssttyyy.png"是空白的,因为它正在保存尚未绘制任何内容的新图形.

In your code, 'tesssttyyy.png' is blank because it is saving the new figure, to which nothing has been plotted.

这篇关于Matplotlib (pyplot) savefig 输出空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆