Python:无法将图保存为png

时间:2016-02-28 15:03:35

标签: python matplotlib word-cloud

我正在尝试将pyplot图保存为png

    import matplotlib.pyplot as plt

    # take relative word frequencies into account, lower max_font_size
    wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
    fig = plt.figure()

    fig.savefig("../../results/plots/"+wf+".png")

    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()
    plt.close()

图表显示正确,但始终存储空图像。我的语法有问题吗?

1 个答案:

答案 0 :(得分:2)

谢谢@tcaswell,你是对的。解决方案是

fig = plt.figure()

plt.imshow(wordcloud)
plt.axis("off")
fig.savefig("../../results/plots/"+wf+".png")
plt.show()
plt.close()

我在绘图之前试图保存