python词云图怎么做

2022年7月25日 23点热度 0人点赞

如何使用python语言制作简单的词云图片。

 

工具/原料

  • python3.6
  • vs code及python插件
  • WordCloud1.6.0

方法/步骤

  1. 1
    安装python3.6

    67python安装教程

  2. 2

    安装vscode并配置python插件

     

    10Visual Studio Code Python开发环境设置

  3. 3

    安装WordCloud1.6.0。

    1.win+R打开运行,输入cmd进入命令行。

    2.输入pip install wordcloud,回车安装。

  4. 4

    新建一个名为wordcloud的文件夹,文件夹下新建main.py文件。在此文件夹下右键,点击open with code,用vs code打开项目。

     

  5. 5

    编辑main.py,写入如下代码:

    import wordcloud

     

    word = wordcloud.WordCloud().generate(

        '''

            When I do count the clock that tells the time,

            And see the brave day sunk in hideous night;

            When I behold the violet past prime,

            And sable curls all silver'd o'er with white:

            When lofty trees I see barren of leaves,

            Which erst from heat did canopy the herd,

            And summer's green, all girded up in sheaves,

            Born on the bier with white and bristly beard;

            Then of thy beauty do I question make,

            That thou among the wastes of time must go,

            Since sweets and beauties do themselves forsake,

            And die as fast as they see others grow;

            And nothing 'gainst Time's scythe can make defence

            Save breed, to brave him when he takes thee hence.

        '''

    )  # 三引号之间的文本,换成你想要的文本

    pic = word.to_image()

    pic.show()

  6. 6

    在vscode的编辑页面右上角有一个绿色箭头标志,点击即可运行代码,得到词云图片。

     

    END

注意事项

  • 高级应用,请参考官网教程:https://github.com/amueller/word_cloud
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。
展开阅读全部

laozhao

这个人很懒,什么都没留下

文章评论