写一个python代码 将dataframe里面的单词以dataframe中的顺序为权重 生成词云图 其中权重在前2000的单词用彩色,剩下的用灰色
时间: 2023-03-10 18:10:57 浏览: 132
python生成词云图
5星 · 资源好评率100%
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordClouddata = pd.read_csv("words.csv") # 读取words.csv数据
words = data['words']
weights = data['weights'] word_dict = {}
for word, weight in zip(words, weights):
word_dict[word] = weight wordcloud = WordCloud(background_color="white",width=1000, height=860, margin=2).generate_from_frequencies(word_dict) # 颜色设置
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
if weights[words.index(word)]>=2000:
color = '#FF0000'
else:
color = '#A9A9A9'
return colorwordcloud.recolor(color_func=grey_color_func, random_state=3)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
阅读全文