提高词云+matplotlib图像的图像质量

huangapple go评论61阅读模式
英文:

Increasing image quality in a wordcloud+matplotlib image

问题

我使用Python的WordCloud和Matplotlib包创建了一个词云,但无法下载高质量的图像。
如果我稍微放大,就看不清楚小字了。我希望在放大时能够理解所有的单词,如果不能,我想要删除它们。

我如何提高这个图像的质量?我尝试增加dpi,增加原始图像的大小,并将图像类型更改为pdf。我认为低质量来自我的词云而不是Matplotlib。

这是我的图像:
[![点击查看图像描述](https://i.stack.imgur.com/nGAQP.png)](https://i.stack.imgur.com/nGAQP.png)

如果我稍微放大,我看到的是:
[![点击查看图像描述](https://i.stack.imgur.com/XZNao.png)](https://i.stack.imgur.com/XZNao.png)

这是我的代码:

```python
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上周在Twitter的活动', fontdict={'fontsize': 15},
             loc='left', pad=20, color='black')

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

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:
提高词云+matplotlib图像的图像质量

And this is what I find if I zoom a bit:
提高词云+matplotlib图像的图像质量

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

答案1

得分: 1

虽然没有直接增加分辨率的选项,但你可以在初始化时增加图的大小来提高绘图的尺寸。这样应该会得到更好质量的图片,因为文件的大小会增加。

使用

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

来将图的大小增加到一个较大的数值(在这个例子中是20),然后检查图片的质量。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月17日 13:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定