pythonmatplotlibword-cloud

Increasing image quality in a wordcloud+matplotlib image


I have a wordcloud created in Python with WordCloud and Matplotlib packages, but I can't download a High quality image of it. If I zoom a bit, I can not understand any of the small words. I would like to be able to understand all the words if I zooz, if not I would like to remove them.

How can I increase this image quality? I tried to do It increasing dpi, increasing the original image's size and changing the image type to pdf. I think the low quality comes from my wordcloud and not from Matplotlib.

This is my image: enter image description here

And this is what I find if I zoom a bit: enter image description here

This is my code:

import pandas as pd
from PIL import Image
import numpy as np
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from collections import Counter


df=pd.read_csv('word_freq_counter.csv')

word_freq_counter = Counter(dict(zip(df['Item'], df['Count'])))


mask = np.array(Image.open('fnatic_2.png'))

fig, ax = plt.subplots()

wc = WordCloud(

    relative_scaling=0.3,
    repeat=True,
    colormap='Oranges',
    font_path='C:/Windows/Fonts/seguiemj.ttf',
    mask=mask, background_color="grey",
    max_words=2000, max_font_size=256,
    min_font_size=3,
    random_state=42, width=mask.shape[1],
    height=mask.shape[0])
wc.generate_from_frequencies(word_freq_counter)

plt.imshow(wc, interpolation="bilinear")


fig.set_facecolor('gray')

ax.set_title('Fnatic\'s last week in Twitter', fontdict={'fontsize': 15},
             loc='left', pad=20, color='black')

plt.axis('off')
plt.savefig("result.png",dpi=300)
plt.show()

Solution

  • While there is no option to directly increase resolution, you can increase the figure size at initialization to increase the size of the plot. This should give you better quality pic as the size of the file will increase...

    Use

    fig, ax = plt.subplots(figsize=(20,20))
    

    to increase the figure size to a large number (20 in this case) and check the quality of the picture.