wordcloud用来制作中文词云

1. 读入数据,删除NAN,用jieba分词
df = pd.read_csv("./data/entertainment_news.csv", encoding='utf-8')
df
df = df.dropna()
df
content=df.content.values.tolist()
content
#jieba.load_userdict(u"data/user_dic.txt")
segment=[]
for line in content:
    try:
        segs=jieba.lcut(line)
        for seg in segs:
            if len(seg)>1 and seg!='\r\n':
                segment.append(seg)
    except:
        print line
        continue

2. 去掉停用词
words_df=pd.DataFrame({'segment':segment})
#words_df.head()
stopwords=pd.read_csv("data/stopwords.txt",index_col=False,quoting=3,sep="\t",names=['stopword'], encoding='utf-8')#quoting=3全不引用
#stopwords.head()
words_df=words_df[~words_df.segment.isin(stopwords.stopword)]
words_df

3. 统计计数

       words_stat=words_df.groupby(by=['segment'])['segment'].agg({"计数":numpy.size})

words_stat=words_stat.reset_index().sort_values(by=["计数"],ascending=False)
words_stat.head()

4. 绘图

wordcloud=WordCloud(font_path="data/simhei.ttf",background_color="white",max_font_size=80)
word_frequence = {x[0]:x[1] for x in words_stat.head(1000).values}
wordcloud=wordcloud.fit_words(word_frequence)
plt.imshow(wordcloud)



Python中,你可以使用`wordcloud`库和`matplotlib`库来创建美观的菱形词云图。`wordcloud`是一个流行的库,用于生成词云图像,而`matplotlib`则提供了丰富的绘图功能。以下是一个简单的步骤来创建菱形词云: 1. 首先,确保你已经安装了`wordcloud`和`matplotlib`库,如果没有,可以通过`pip install wordcloud matplotlib`命令来安装。 ```python import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS ``` 2. 准备数据,通常是一段文本,其中包含你想要展示的词。你也可以从文件或API获取数据。 ```python text = "这里是你想要用来生成词云的文本内容,可以是文章、歌词或其他大量的文本数据" ``` 3. 创建停用词列表,以排除常见的无意义词汇如“的”、“是”等。 ```python stopwords = set(STOPWORDS) ``` 4. 使用`WordCloud`类定义词云的样式和参数,比如形状、背景颜色、最大单词大小等。 ```python wc = WordCloud( width=800, height=800, background_color="white", stopwords=stopwords, mask=np.array(Image.open("diamond_shape.png")), # 使用钻石形状的图片作为掩模 contour_width=3, contour_color='steelblue', ) ``` 5. 使用文本生成词云。 ```python wc.generate(text) ``` 6. 显示词云。 ```python plt.figure(figsize=(8, 8), facecolor=None) # 图像大小 plt.imshow(wc, interpolation="bilinear") plt.axis("off") # 关掉坐标轴 plt.title("菱形词云图", fontsize=20) plt.show() ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Soyoger

听说打赏的都进了福布斯排行榜。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值