WordCloud蒙版失效求助:蒙版仅显轮廓,文字填充整个画布
词云蒙版失效问题求助
问题描述
使用WordCloud生成词云时,蒙版仅显示轮廓点,文字填充整个画布而非贴合蒙版形状,此前从未出现该问题,暂未定位原因。更换其他PNG蒙版后问题依旧。
实现流程
- 导入依赖库
# import libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
- 数据读取与说明
读取cleanTwitterData.csv,数据包含Date、Close、name、tweet字段,示例数据如下:
data = pd.read_csv('cleanTwitterData.csv') data
| Date | Close | name | tweet |
|---|---|---|---|
| 2022-04-25 | 51.700001 | elonmusk | hope that even worst critics remain Twitter be... |
| 2022-04-26 | 49.680000 | elonmusk | esaagar Suspending the Twitter account major n... |
| 2022-04-27 | 48.639999 | elonmusk | For Twitter deserve public trust must politica... |
| ... | ... | ... | ... |
| 2022-10-28 | 53.700001 | elonmusk | Comedy now legal Twitter |
- 蒙版读取与数组信息
使用twitterLogo.png作为蒙版,读取后的numpy数组如下:
mask = np.array(Image.open("twitterLogo.png")) mask
array([[[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [238, 238, 238], [238, 238, 238], [239, 239, 239]], [[255, 255, 255], [254, 254, 254], [254, 254, 254], ..., [237, 237, 237], [237, 237, 237], [238, 238, 238]], ..., [254, 254, 254], [254, 254, 254], [255, 255, 255]], [[239, 239, 239], [238, 238, 238], [238, 238, 238], ..., [255, 255, 255], [255, 255, 255], [255, 255, 255]]], dtype=uint8)
- 词云生成代码
#Generate a word cloud image text = " ".join(i for i in data.tweet) stopwords = set(STOPWORDS) wordcloud = WordCloud(stopwords=stopwords,background_color='white', max_words=1000, mask=mask,contour_color='#023075',contour_width=3,colormap='Blues').generate(text) plt.figure() plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") plt.show()
实际现象
生成的词云为矩形画布,仅蒙版轮廓显示为蓝色点,文字未贴合蒙版形状,更换其他PNG蒙版问题仍存在。
内容的提问来源于stack exchange,提问作者DankyKang
相关产品推荐
相关产品推荐

