0

I just want to save a simple wordcloud in a file 200x150px but am getting 640x480 pixel instead. What am I doing wrong?

from matplotlib import pyplot as plt
from wordcloud import WordCloud
cloud = WordCloud(width=200,height=150)
plt.imshow(cloud.generate_from_frequencies(t))
plt.savefig('c.png')
plt.clf()
Diego
  • 812
  • 7
  • 25

1 Answers1

3

In order to save the figure with the exact required number of pixel you can see the post about Specifying and saving a figure with exact size in pixels.

It will give you:

from matplotlib import pyplot as plt
from wordcloud import WordCloud
cloud = WordCloud(width=200/my_dpi,height=150/my_dpi)
plt.imshow(cloud.generate_from_frequencies(t))
plt.savefig('c.png', dpi=my_dpi)
plt.clf()

with the value of my_dpi equal to the dpi of your monitor. You can find it following this link for example.

  • so my_dpi = 96 for example :-) Thanks for the solution! Even though the pyplot approach seems to be braindamaged. But maybe I am missing their motivation altogether. – Diego Feb 21 '19 at 21:39
  • Nice. Perfect scaling, used 200dpi for inserting into document. Tried several other things. Changing width and height on wordcloud didnt change anything. changing plt figsize = (20,10) created a big white picture without any content. – Kaspatoo Nov 04 '21 at 22:55