Generating a square wordcloud from the US constitution using default arguments.
from os import path
import matplotlib.pyplot as plt
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
wordcloud = WordCloud().generate(text)
# Open a plot of the generated image.
You can't increase the resolution of the image in
plt.show() since that is determined by your screen, but you can increase the size. This allows it to scale, zoom, etc. without blurring. To do this pass dimensions to
wordcloud = WordCloud(width=800, height=400).generate(text)
However, this just determines the size of the image created by
WordCloud. When you display this using
matplotlib it is scaled to the size of the plot canvas, which is (by default) around 800x600 and you again lose quality. To fix this you need to specify the size of the figure before you call
plt.figure( figsize=(20,10) ) plt.imshow(wordcloud)
By doing this I can successfully create a 2000x1000 high resolution word cloud.
For your second question (removing the border) first we could set the border to black, so it is less apparent, e.g.
plt.figure( figsize=(20,10), facecolor='k' )
You can also shrink the size of the border by using
The final code:
# Read the whole text. text = open(path.join(d, 'constitution.txt')).read() wordcloud = WordCloud(width=1600, height=800).generate(text) # Open a plot of the generated image. plt.figure( figsize=(20,10), facecolor='k') plt.imshow(wordcloud) plt.axis("off") plt.tight_layout(pad=0) plt.show()
By replacing the last two lines with the following you can get the final output shown below:
plt.savefig('wordcloud.png', facecolor='k', bbox_inches='tight')