1.词云图(已知词频)
1.1不加背景图层
1.1.1原始数据
![python绘制词云图(已知词频以及未知词频)_第1张图片](http://img.e-com-net.com/image/info8/69170da97c244fb597e37bfd09b27961.jpg)
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效果
![python绘制词云图(已知词频以及未知词频)_第2张图片](http://img.e-com-net.com/image/info8/cbe6647f7d2c429cb7ece2c549547f2a.jpg)
1.2加背景图层
1.2.1原始数据
![python绘制词云图(已知词频以及未知词频)_第3张图片](http://img.e-com-net.com/image/info8/0dbfe8cd9a1746658c327d771f5dfe8f.jpg)
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效果
![python绘制词云图(已知词频以及未知词频)_第4张图片](http://img.e-com-net.com/image/info8/dc0410423a9545a7b7ec49f206530a09.jpg)
2.词云图(未知词频)
2.1不加背景图层
2.1.1原始数据
![python绘制词云图(已知词频以及未知词频)_第5张图片](http://img.e-com-net.com/image/info8/e7d75bab2e0f4ae5b17cf22e597f6686.jpg)
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效果
![python绘制词云图(已知词频以及未知词频)_第6张图片](http://img.e-com-net.com/image/info8/bdb6e0972c8549c6a60461fdd2411748.jpg)
2.2加背景图层
2.2.1原始数据
![python绘制词云图(已知词频以及未知词频)_第7张图片](http://img.e-com-net.com/image/info8/7d224c127cbf45f595a6e4570ee62e77.jpg)
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效果
![python绘制词云图(已知词频以及未知词频)_第8张图片](http://img.e-com-net.com/image/info8/b253492baeda44aaacda30b331888f15.jpg)