偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python數(shù)據(jù)可視化:WordCloud入門

大數(shù)據(jù) 數(shù)據(jù)可視化
WordCloud是一種很好的展現(xiàn)數(shù)據(jù)的方式,網(wǎng)上也有不少小工具和在線網(wǎng)頁。但是有些不支持中文,有些安裝復(fù)雜,所以決定用Python實(shí)現(xiàn)。主要參考官網(wǎng),通過官網(wǎng)的例子,講一下WordCloud的制作。

WordCloud是一種很好的展現(xiàn)數(shù)據(jù)的方式,網(wǎng)上也有不少小工具和在線網(wǎng)頁。

但是有些不支持中文,有些安裝復(fù)雜,所以決定用Python實(shí)現(xiàn)。

主要參考官網(wǎng),通過官網(wǎng)的例子,講一下WordCloud的制作。

主要流程

  • 獲取內(nèi)容的路徑
  • 如果是一段文字,系統(tǒng)自動(dòng)算頻次
  • 你也可以直接導(dǎo)入統(tǒng)計(jì)好的頻次
  • 設(shè)置字體
  • 一般字體路徑在C:\Windows\Fonts,你可以選自己喜歡的中文或者英文字體
  • 切割中文字符
  • 英文字符就不用切割了
  • 輸入WordCloud的參數(shù)
  • 背景色
  • 字號(hào)
  • 生成的形狀
  • 顏色
  • 字體大小
  • 字體旋轉(zhuǎn)等等
  • 生成WordCloud
  • 用matplotlib顯示圖片

效果圖

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

安裝庫

老規(guī)矩,首先,你要安裝庫。

最基本的兩個(gè):

 

  1. pip install wordcloud #這是WordCloud的庫 
  2.  
  3. pip install matplotlib #顯示圖像 

一個(gè)單詞構(gòu)造WordCloud

在這個(gè)代碼中,我們需要安裝一個(gè)numpy庫

(大部分小伙伴應(yīng)該都裝過,就不用再裝了)

  1. pip install numpy 

這里用這個(gè)庫,主要是想用數(shù)學(xué)坐標(biāo)生成一個(gè)簡單的背景圖案,比如圓形、方形

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

基本步驟

  • 輸入單詞
  • 用numpy 生成一個(gè)形狀,下面生成了一個(gè)圓形mask
  • 輸入WordCloud的參數(shù)(包括背景色、是否重復(fù)、圖案形狀)
  • 用matplotlib顯示圖片
  1. import numpy as np 
  2. import matplotlib.pyplot as plt 
  3. from wordcloud import WordCloud 
  4. text = "square" #輸入你要的單詞 
  5. x, y = np.ogrid[:300, :300] #快速產(chǎn)生一對(duì)數(shù)組 
  6. # 產(chǎn)生一個(gè)以(150,150)為圓心,半徑為130的圓形mask 
  7. mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2 #此時(shí)mask是bool型 
  8. mask = 255 * mask.astype(int) #變量類型轉(zhuǎn)換為int型 
  9. wc = WordCloud( 
  10.  background_color="white", #背景顏色為“白色” 
  11.  repeat=True, #單詞可以重復(fù) 
  12.  mask=mask #指定形狀,就是剛剛生成的圓形 
  13.  ) 
  14. wc.generate(text) #從文本生成wordcloud 
  15. plt.axis("off") #把作圖的坐標(biāo)軸關(guān)掉 
  16. plt.imshow(wc, interpolation="bilinear"
  17. plt.show() 

生成WordCloud

最簡單的生成方式,文本內(nèi)容都是英文,直接用系統(tǒng)默認(rèn)的形狀(一個(gè)長方形)

我這邊是導(dǎo)入了一個(gè)商務(wù)英語的txt,所以可以看到,出現(xiàn)次數(shù)最多的單詞是company,然后是business、new、work等單詞,我還看到了money,哈哈~

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

基本步驟

  • 獲取內(nèi)容txt的路徑
  • 輸入WordCloud的參數(shù)(包括背景色、字號(hào)等)
  • 生成WordCloud
  • 用matplotlib顯示圖片

*WordCloud有很多參數(shù),如果你不寫,都是默認(rèn)的。比如背景色默認(rèn)黑色。

  1. import os 
  2. from os import path 
  3. from wordcloud import WordCloud 
  4. from matplotlib import pyplot as plt 
  5. # 獲取當(dāng)前文件路徑 
  6. d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 
  7. # 獲取文本txt的路徑(txt和代碼在一個(gè)路徑下面) 
  8. text = open(path.join(d,'BusinessEnglish.txt')).read() 
  9. # 生成詞云 
  10. wc = WordCloud( 
  11.  scale=2, 
  12.  max_font_size=100, #最大字號(hào) 
  13.  background_color='white' #設(shè)置背景顏色 
  14.  ) 
  15. wc.generate(text) # 從文本生成wordcloud 
  16. # wc.generate_from_text(text) #用這種表達(dá)方式也可以  
  17. # 顯示圖像 
  18. plt.imshow(wc,interpolation='bilinear'
  19. plt.axis('off'
  20. plt.tight_layout() 
  21. wc.to_file('標(biāo)簽云效果圖.png') # 儲(chǔ)存圖像 
  22. #plt.savefig('標(biāo)簽云效果圖.png',dpi=200) #用這個(gè)可以指定像素 
  23. plt.show() 

設(shè)置WordCloud形狀

都是長方形、圓形、正方形這種,好像不夠炫酷

為了炫酷,我們可以給它設(shè)置不同的形狀,比如云朵、愛心等等

下面我們用Alice的小裙子做個(gè)實(shí)例

這個(gè)圖片長這樣

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

我們生成的圖片是這樣的,可以看到,完整保留了上圖的輪廓

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

基本步驟

和之前基本都是一致的,就是多了一步,導(dǎo)入指定圖片,獲取圖片輪廓

  • 獲取內(nèi)容txt的路徑
  • 輸入WordCloud的參數(shù)(包括背景色、字號(hào)等),指定了生成的形狀
  • 生成WordCloud
  • 用matplotlib顯示圖片

*代碼中增加了一個(gè)stopwords,有些你覺得沒意義的單詞,不想顯示在圖片上,你就可以放在這里

  1. from os import path 
  2. from PIL import Image 
  3. import numpy as np 
  4. import matplotlib.pyplot as plt 
  5. import os 
  6. from wordcloud import WordCloud, STOPWORDS 
  7. # 獲取當(dāng)前文件路徑 
  8. d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 
  9. # 獲取文本txt的路徑(txt和代碼在一個(gè)路徑下面) 
  10. text = open(path.join(d, 'BusinessEnglish.txt')).read() 
  11. # 讀取mask的圖像(圖像和代碼在一個(gè)路徑下面) 
  12. alice_mask = np.array(Image.open(path.join(d, "alice_mask.png"))) 
  13. # 設(shè)置不顯示單詞,比如said、inon、is這種單詞 
  14. stopwords = set(STOPWORDS) 
  15. stopwords.add("said"
  16. # 設(shè)置詞云參數(shù) 
  17. wc = WordCloud(background_color="white",  
  18.  max_words=2000,  
  19.  mask=alice_mask, 
  20.  stopwords=stopwords,  
  21.  contour_width=3, #設(shè)置輪廓寬度 
  22.  contour_color='steelblue') #設(shè)置輪廓顏色 
  23. # 從文本生成wordcloud 
  24. wc.generate(text) 
  25. # 保存到文件 
  26. wc.to_file(path.join(d, "alice.png")) 
  27. # 顯示圖片 
  28. plt.imshow(wc, interpolation='bilinear'
  29. plt.axis("off"
  30. plt.figure() #新建一個(gè)圖片,把mask也顯示出來 
  31. plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear'
  32. plt.axis("off"
  33. plt.show() 

根據(jù)圖片調(diào)整顏色

原圖是這樣的

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

如果我們直接根據(jù)上一步,獲取圖片輪廓,可以得到下圖

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

我們進(jìn)一步,根據(jù)原圖,調(diào)整顏色

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

其實(shí)就是獲取了圖片顏色,也是一行代碼

  1. image_colors = ImageColorGenerator(alice_coloring) 

完整代碼

 

  1. from os import path  
  2. from PIL import Image  
  3. import numpy as np  
  4. import matplotlib.pyplot as plt  
  5. import os  
  6. from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator 
  7.  
  8. # 獲取當(dāng)前文件路徑 
  9.  
  10. d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()  
  11. # 獲取文本txt的路徑(txt和代碼在一個(gè)路徑下面)  
  12. text = open(path.join(d, 'BusinessEnglish.txt')).read() 
  13.  
  14. # 讀取我要的圖片文件  
  15. alice_coloring = np.array(Image.open(path.join(d, "alice_color.png"))) 
  16.  
  17. # 設(shè)置不顯示的單詞 
  18.  
  19. stopwords = set(STOPWORDS)  
  20. stopwords.add("said"
  21.  
  22. # 設(shè)置詞云參數(shù) 
  23.  
  24. wc = WordCloud(background_color="white" 
  25. max_words=2000, 
  26.  
  27. mask=alice_coloring,  
  28. stopwords=stopwords,  
  29. max_font_size=40,  
  30. random_state=42)  
  31. # 從文本生成wordcloud  
  32. wc.generate(text) 
  33.  
  34. # 根據(jù)圖片,創(chuàng)建顏色 
  35.  
  36. image_colors = ImageColorGenerator(alice_coloring)  
  37. # 把圖片分成3份 
  38.  
  39. fig, axes = plt.subplots(1, 3)  
  40. axes[0].imshow(wc, interpolation="bilinear" 
  41. # recolor wordcloud and show  
  42. # we could also give color_func=image_colors directly in the constructor  
  43. axes[1].imshow(wc.recolor(color_func=image_colors), interpolation="bilinear" 
  44. axes[2].imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear" 
  45. for ax in axes:  
  46. ax.set_axis_off() 

 

  1. # 單獨(dú)顯示圖片  
  2. # plt.figure()  
  3. # plt.imshow(wc, interpolation="bilinear" 
  4. # plt.axis("off" 
  5. # plt.figure()  
  6. # plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear" 
  7. # plt.axis("off" 
  8. # plt.figure()  
  9. # plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear" 
  10. # plt.axis("off" 
  11. plt.show() 

用頻率繪制WordCloud

上面是直接把一個(gè)txt輸進(jìn)去,系統(tǒng)自動(dòng)給你算出現(xiàn)次數(shù)的

但是實(shí)際過程中,我們有時(shí)候,是知道單詞出現(xiàn)次數(shù)的,我們就想根據(jù)已知的次數(shù)顯示

這一步,其實(shí)就改了一行代碼,

原來是這樣的

  1. wc.generate(text) # 這里的text是一段文字 

現(xiàn)在是這樣的

 

  1. wc.generate_from_frequencies(text) 
  2.  
  3. # 這里的text是一個(gè)字典 
  4.  
  5. 'ken': 1, 'was': 47, 'hot': 2, 'water': 2 

如果你已經(jīng)有一個(gè)字典,直接代進(jìn)去就好了

這里給大家詳細(xì)看一下,如果假設(shè)我沒有這個(gè)字典

我還是一段文字,我想先生成這個(gè)字典,再代入進(jìn)去

這里,你需要安裝一個(gè)庫multidict,創(chuàng)建一鍵多值字典

  1. pip install multidict 

用multidict這個(gè)庫,我可以把文本變成一個(gè)字典

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

通過上圖就可以看到,這個(gè)字典有1105個(gè)組合,每一個(gè)單詞,都統(tǒng)計(jì)了出現(xiàn)次數(shù)

  1. import multidict as multidict 
  2. import numpy as np 
  3. import os 
  4. import re 
  5. from PIL import Image 
  6. from os import path 
  7. from wordcloud import WordCloud 
  8. import matplotlib.pyplot as plt 
  9. def getFrequencyDictForText(sentence): 
  10.  fullTermsDict = multidict.MultiDict() 
  11.  tmpDict = {} 
  12.  # making dict for counting frequencies 
  13.  for text in sentence.split(" "): 
  14.  if re.match("a|the|an|the|to|in|for|of|or|by|with|is|on|that|be", text): 
  15.  continue 
  16.  val = tmpDict.get(text, 0) 
  17.  tmpDict[text.lower()] = val + 1 
  18.  for key in tmpDict: 
  19.  fullTermsDict.add(key, tmpDict[key]) 
  20.  return fullTermsDict 
  21. def makeImage(text): 
  22.  alice_mask = np.array(Image.open("alice_mask.png")) 
  23.  wc = WordCloud( 
  24.  background_color="white"
  25.  max_words=1000,  
  26.  mask=alice_mask 
  27.  ) 
  28.  # generate word cloud 
  29.  wc.generate_from_frequencies(text) 
  30.  # show 
  31.  plt.imshow(wc, interpolation="bilinear"
  32.  plt.axis("off"
  33.  wc.to_file('frequency.png') # 儲(chǔ)存圖像 
  34.  plt.show() 
  35. # 獲取當(dāng)前文件路徑 
  36. d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 
  37. # 獲取文本txt的路徑(txt和代碼在一個(gè)路徑下面) 
  38. text = open(path.join(d, 'BusinessEnglish.txt'), encoding='utf-8'
  39. text = text.read() 
  40. makeImage(getFrequencyDictForText(text)) 

生成中英文WordCloud

生成一個(gè)中英文混搭的WordCloud

如果你的txt全是中文,那就是全中文的

 

Python數(shù)據(jù)可視化:WordCloud入門(大伙都在用)

基本步驟

和之前基本也都一樣,就是中文字符,需要增加一個(gè)中文詞語切割

這就需要添加一個(gè)庫

  1. pip install jieba # 中文切割 
  1. 獲取內(nèi)容txt的路徑
  2. 設(shè)置字體
  3. 切割中文字符
  4. 輸入WordCloud的參數(shù)(包括背景色、字號(hào)等),指定了生成的形狀
  5. 生成WordCloud
  6. 用matplotlib顯示圖片
  1. import os 
  2. from os import path 
  3. from wordcloud import WordCloud 
  4. from matplotlib import pyplot as plt 
  5. import jieba 
  6. # 獲取當(dāng)前文件路徑 
  7. d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() 
  8. # 獲取文本txt 
  9. text = open(path.join(d,'商務(wù)英語.txt'),encoding='utf-8').read() 
  10. # 設(shè)置中文字體 
  11. font_path = 'C:\Windows\Fonts\\simfang.ttf' # 字體路徑 
  12. # 精確切割中文字符 
  13. text = ' '.join(jieba.cut(text, cut_all = False)) 
  14. # 生成詞云 
  15. wc = WordCloud( 
  16.  font_path = font_path, #字體路徑 
  17.  scale=2, 
  18.  max_words = 100, #最多詞個(gè)數(shù) 
  19.  max_font_size=100, #最大字號(hào) 
  20.  background_color='white' #背景色 
  21.  ) 
  22. wc.generate(text) 
  23. # 顯示圖像 
  24. plt.imshow(wc,interpolation='bilinear'
  25. plt.axis('off'
  26. plt.tight_layout() 
  27. # 儲(chǔ)存圖像 
  28. #wc.to_file('標(biāo)簽云效果圖.png'
  29. #plt.savefig('標(biāo)簽云效果圖.png',dpi=200) 
  30. plt.show() 

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2025-10-10 07:00:00

Python數(shù)據(jù)可視化數(shù)據(jù)分析

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2019-03-05 09:20:47

Vim可視化模式命令

2022-08-26 09:15:58

Python可視化plotly

2022-02-23 09:50:52

PythonEchartspyecharts

2017-09-13 14:06:32

數(shù)據(jù)可視化數(shù)據(jù)圖表數(shù)據(jù)

2024-08-20 18:16:49

數(shù)據(jù)可視化Python

2017-10-31 09:38:53

大數(shù)據(jù)數(shù)據(jù)可視化Python

2018-11-30 10:28:44

Python反爬網(wǎng)頁

2021-02-07 20:23:09

GoogeBlockly可視化編程

2015-08-20 10:00:45

可視化

2018-12-03 16:50:23

數(shù)據(jù)可視化數(shù)據(jù)分析薪水

2019-01-02 11:59:26

Python數(shù)據(jù)可視化GDP

2018-11-21 14:38:09

分析在數(shù)據(jù)電影

2022-09-29 11:16:21

Python數(shù)據(jù)可視化

2019-01-21 15:10:11

佩奇可視化數(shù)據(jù)

2018-03-07 11:35:49

Python可視化數(shù)據(jù)

2017-06-29 11:26:08

Python數(shù)據(jù)可視化

2020-09-02 13:56:03

Python可視化數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)