Bobo
Bobo

Reputation: 971

Increase resolution with word-cloud and remove empty border

I am using word cloud with some txt files. How do I change this example if I wanted to 1) increase resolution and 2) remove empty border.

#!/usr/bin/env python2
"""
Minimal Example
===============
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.
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

Upvotes: 56

Views: 63899

Answers (6)

teddy waweru
teddy waweru

Reputation: 81

In case you run into the issue of slower application while improving the resolution ie. in a web application, the WordCloud documentation advises that you utilize the scale parameter along with the canvas' width & height params to get a resolution & response time that works for your use case.

Upvotes: 0

Zack Light
Zack Light

Reputation: 333

You can use the method to_svg and get a resolution however high you want.

    with open("Output.svg", "w") as text_file:
    text_file.write(wc.to_svg())

Try an example by appending these two lines to this file, and the result is gorgeous.

(Other answers have addressed the border problem, and also the example doe not have a border.)

Upvotes: 1

Naved Ahmad
Naved Ahmad

Reputation: 821

It is very simple, plt.tight_layout(pad=0) does the job, reduces the space in the background, removing the excess padding.

Upvotes: 1

Silver
Silver

Reputation: 141

If you are trying to use an image as a mask, make sure to use a big image to get better image quality.. I spent hours figuring this out.

Heres an example of a code snippet I used

mask = np.array(Image.open('path_to_your_image'))
image_colors = ImageColorGenerator(mask)
wordcloud = WordCloud(width=1600, height=800, background_color="rgba(255, 255, 255, 0)", mask=mask
                     ,color_func = image_colors).generate_from_frequencies(x)

# Display the generated image:
plt.figure( figsize=(20,10) )
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

Upvotes: 5

Jeff J.
Jeff J.

Reputation: 75

Blurry wordclouds - I've been wrestling with this. For my use, I found that too large a differential in the between the most frequent word occurrences and those with few occurrences left the lower-count words unreadable. When I scaled the more frequent counts to reduce the differential, all the lower-frequency words were much more readable.

Upvotes: -2

mfitzp
mfitzp

Reputation: 15545

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, e.g.

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 imshow, e.g.

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 tight_layout, e.g.

plt.tight_layout(pad=0)

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

final Constitution wordcloud

Upvotes: 120

Related Questions