jupyter詞雲代碼(詞雲inline)

在jupyter生成了一個詞雲,怎麼保存?怎麼顯示在jupyter頁面(不用再額外打開圖片)呢?

先看生成詞雲的代碼(參考鏈接:https://www.cnblogs.com/ZaraNet/p/10136589.html

from wordcloud import WordCloud
import PIL .Image as image
import numpy as np
import jieba

def trans_CN(text):
    word_list = jieba.cut(text)
    # 分詞後在單獨個體之間加上空格
    result = " ".join(word_list)
    return result;


with open("F:\\minister.txt") as fp:
    text = fp.read()
    text  = trans_CN(text)
    # print(text)
    mask = np.array(image.open("F:\\20180612151652413.png"))
    wordcloud = WordCloud(
        mask=mask,
        font_path = "C:\\Windows\\Fonts\\msyh.ttc"
    ).generate(text)
    image_produce = wordcloud.to_image()
    image_produce.show()

大家在運行上面的代碼之後,會彈出圖片,並且如果忘了保存,又不能找到。

想要在jupyter頁面顯示圖片,並自動保存,只需要加一個cell運行下面代碼即可:

wordcloud.to_file('ciyun.png')

from IPython.display import Image
Image('ciyun.png')

默認保存在ipynb文件所在的目錄下哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章