AbtPst
AbtPst

Reputation: 8018

Python : Cannot save plots as png

I am trying to save pyplot plots as png as

    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()

The plot is shown correctly but it always stores an empty image. Is something wrong with my syntax?

Upvotes: 2

Views: 4316

Answers (1)

AbtPst
AbtPst

Reputation: 8018

thanks @tcaswell, you are right. the solution is

fig = plt.figure()

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

i was trying to save before plotting

Upvotes: 3

Related Questions