• 技术文章 >常见问题 >Python常见问题

    python词云安装什么库

    yangyang2020-05-08 13:18:57原创3712

    python词云需要安装wordcloud库。

    安装方法:

    在cmd使用pip install wordcloud命令即可安装。

    wordcloud库把词云当作一个WordCloud对象:wordcloud.WordCloud()代表一个文本对应的词云。

    可以根据文本中词语出现的频率等参数绘制词云。

    示例:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    from wordcloud import WordCloud

     

    f = open(u'D:\\PythonStudio\\WORK\\Demo1\\test.txt','r').read()

    wordcloud = WordCloud(background_color="black",width=1000, height=860, margin=2).generate(f)

     

    #width,height,margin可以设置图片属性

     

    # generate 可以对全部文本进行自动分词,但是他对中文支持不好,对中文的分词处理请看我的下一篇文章

    #wordcloud =WordCloud(font_path = r'D:\Fonts\simkai.ttf').generate(f)

    # 你可以通过font_path参数来设置字体集

     

    #background_color参数为设置背景颜色,默认颜色为黑色

     

    import matplotlib.pyplot as plt

    plt.imshow(wordcloud)

    plt.axis("off")

    plt.show()

     

    wordcloud.to_file('test.png')

    更多Python知识请关注Python视频教程栏目。

    专题推荐:python
    上一篇:python如何判断字符串中是否含有空格? 下一篇:python怎样播放视频?

    相关文章推荐

    • 如何运行atom中的python程序?• python怎么取矩阵的转置?• python如何查找在线帮助?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网