1.词云图(已知词频)
1.1不加背景图层
1.1.1原始数据
![数据源](https://i-blog.csdnimg.cn/blog_migrate/b9249b6289f9d1350fee8ecbfc80522d.png)
1.1.2程序
import pandas as pd
from wordcloud import WordCloud
from matplotlib import pyplot as plt
pd.set_option('display.float_format', lambda x: '%.4f' % x)
data=pd.read_excel('./GDP.xlsx',sheet_name='2021',usecols=['国家/地区','GDP总量(人民币核算)'])
print(data.head())
list_country=data['国家/地区'].tolist()
list_gdp=data['GDP总量(人民币核算)'].tolist()
dic=dict(zip(list_country,list_gdp))
print(dic)
font=r'C:\Windows\Fonts\simhei.ttf'
wordcloud=WordCloud(background_color='black',width=800,height=800,margin=1,font_path=font).generate_from_frequencies(dic)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
1.1.3效果
![词云图效果一](https://i-blog.csdnimg.cn/blog_migrate/b9fe20de4e6afd5ed7f6907c6c88ea45.png)
1.2加背景图层
1.2.1原始数据
![数据源](https://i-blog.csdnimg.cn/blog_migrate/f1396d17cc47ae8e976503062d4f2dd5.png)
1.2.2程序
import pandas as pd
import numpy as np
from wordcloud import WordCloud
from matplotlib import pyplot as plt
from PIL import Image
pd.set_option('display.float_format', lambda x: '%.4f' % x)
data=pd.read_excel('./GDP.xlsx',sheet_name='2021',usecols=['国家/地区','GDP总量(人民币核算)'])
print(data.head())
list_country=data['国家/地区'].tolist()
list_gdp=data['GDP总量(人民币核算)'].tolist()
dic=dict(zip(list_country,list_gdp))
print(dic)
font=r'C:\Windows\Fonts\simhei.ttf'
MASK=np.array(Image.open('./rmb.jpg'))
wordcloud=WordCloud(background_color='white',scale=1,max_words=500,max_font_size=100,font_path=font,mask=MASK,repeat=False,mode='RGB',colormap='winter').generate_from_frequencies(dic)
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis('off')
plt.show()
1.2.3效果
![词云图效果二](https://i-blog.csdnimg.cn/blog_migrate/599e63f3cec98f7ba3b4a686405746d3.png)
2.词云图(未知词频)
2.1不加背景图层
2.1.1原始数据
![数据源](https://i-blog.csdnimg.cn/blog_migrate/bce68bec95a0854b8c4bf5341a56f548.png)
2.1.2程序
import re
import jieba
import pandas as pd
from wordcloud import WordCloud
from matplotlib import pyplot as plt
text=pd.read_csv('zfgzbg.txt',index_col=0)
print(text)
text2=str(text)
text3=re.sub("[a-zA-Z0-9'!""#$%&\'()*+,-./:;<=>?@,。?★、…【】《》:?“”‘'![\\]^_`{|}~\s]+","",text2)
print(text3)
text4=jieba.lcut(text3)
print(text4)
text5=' '.join(text4)
print(text5)
stopwords=['我','和','你','的','地','得','了','都','对','向','在','可','能','为','要','再','是','等','一','二','三','四','五','六','七','八','九','十','各位','代表','一年','请予']
font=r'C:\Windows\Fonts\simhei.ttf'
wordcloud=WordCloud(background_color='white',scale=1,max_words=500,max_font_size=100,font_path=font,stopwords=stopwords).generate_from_text(text5)
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis('off')
plt.show()
2.1.3效果
![词云图效果三](https://i-blog.csdnimg.cn/blog_migrate/4599a6ef7eb2115b723a853ef7c22185.png)
2.2加背景图层
2.2.1原始数据
![数据源](https://i-blog.csdnimg.cn/blog_migrate/4085031039cbbf57f9754fcca4766a09.png)
2.2.2程序
import re
import jieba
import pandas as pd
import numpy as np
from PIL import Image
from wordcloud import WordCloud
from matplotlib import pyplot as plt
text=pd.read_csv('zfgzbg.txt',index_col=0)
print(text)
text2=str(text)
text3=re.sub("[a-zA-Z0-9'!""#$%&\'()*+,-./:;<=>?@,。?★、…【】《》:?“”‘'![\\]^_`{|}~\s]+","",text2)
print(text3)
text4=jieba.lcut(text3)
print(text4)
text5=' '.join(text4)
print(text5)
stopwords=['我','和','你','的','地','得','了','都','对','向','在','可','能','为','要','再','是','等','一','二','三','四','五','六','七','八','九','十','各位','代表','一年','请予']
font=r'C:\Windows\Fonts\simhei.ttf'
MASK=np.array(Image.open('./map.jpg'))
wordcloud=WordCloud(background_color='white',scale=2,max_words=500,max_font_size=150,font_path=font,stopwords=stopwords,mask=MASK,colormap='brg').generate_from_text(text5)
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis('off')
plt.show()
2.2.3效果
![词云图效果四](https://i-blog.csdnimg.cn/blog_migrate/9c599d4fcd4781419fe13ebc76b505de.png)