【问题标题】:Increase resolution with word-cloud and remove empty border使用词云提高分辨率并删除空白边框
【发布时间】:2015-05-01 10:22:11
【问题描述】:

我正在使用带有一些 txt 文件的 word cloud。如果我想 1) 提高分辨率和 2) 删除空边框,我该如何更改 this example

#!/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()

【问题讨论】:

    标签: python matplotlib word-cloud


    【解决方案1】:

    很简单,plt.tight_layout(pad=0) 完成这项工作,减少背景空间,去除多余的填充。

    【讨论】:

      【解决方案2】:

      如果您尝试使用图像作为遮罩,请确保使用大图像以获得更好的图像质量。我花了好几个小时才弄清楚这一点。

      这是我使用的代码 sn-p 的示例

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

      【讨论】:

        【解决方案3】:

        模糊的文字云 - 我一直在努力解决这个问题。就我的使用而言,我发现出现频率最高的单词和出现次数很少的单词之间的差异太大,导致计数较低的单词无法阅读。当我缩放更频繁的计数以减少差异时,所有低频词都更具可读性。

        【讨论】:

          【解决方案4】:

          您无法增加plt.show() 中图像的分辨率,因为这是由您的屏幕决定的,但您可以增加尺寸。这允许它在不模糊的情况下进行缩放、缩放等。为此,将尺寸传递给WordCloud,例如

          wordcloud = WordCloud(width=800, height=400).generate(text)
          

          但是,这只是决定了WordCloud 创建的图像的大小。当您使用matplotlib 显示它时,它会缩放到绘图画布的大小,(默认情况下)大约为 800x600,并且您再次失去质量。要解决此问题,您需要在调用 imshow 之前指定图形的大小,例如

          plt.figure( figsize=(20,10) )
          plt.imshow(wordcloud)
          

          通过这样做,我可以成功创建 2000x1000 的高分辨率词云。

          对于您的第二个问题(删除边框),首先我们可以将边框设置为黑色,因此不太明显,例如

          plt.figure( figsize=(20,10), facecolor='k' )
          

          您也可以使用tight_layout 来缩小边框的大小,例如

          plt.tight_layout(pad=0)
          

          最终代码:

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

          通过将最后两行替换为以下内容,您可以获得如下所示的最终输出:

          plt.savefig('wordcloud.png', facecolor='k', bbox_inches='tight')
          

          【讨论】:

            猜你喜欢
            • 2018-04-01
            • 1970-01-01
            • 2020-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-04
            • 2010-10-19
            • 1970-01-01
            相关资源
            最近更新 更多