[英]My wordcloud mask is producing a series of points outlining where the mask should be but the words are fitting to the shape of the entire image
As described above my wordcloud is not behaving in a way I have sen before and I have no idea what is causing the issue as I have made them before and never experienced this problem.如上所述,我的 wordcloud 没有按照我以前见过的方式运行,我不知道是什么导致了这个问题,因为我以前做过,但从未遇到过这个问题。
# 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
This is what my data looks like这就是我的数据的样子
data = pd.read_csv('cleanTwitterData.csv')
data
Date Close name tweet
3 2022-04-25 51.700001 elonmusk hope that even worst critics remain Twitter be...
4 2022-04-26 49.680000 elonmusk esaagar Suspending the Twitter account major n...
5 2022-04-27 48.639999 elonmusk For Twitter deserve public trust must politica...
6 2022-04-28 49.110001 elonmusk Let make Twitter maximum fun
7 2022-04-29 49.020000 elonmusk The people Twitter strongly agree that Twitter...
... ... ... ... ...
176 2022-10-15 50.450001 elonmusk KimDotcom Twitter trying hardest escalate this...
186 2022-10-25 52.779999 elonmusk SwipeWright Twitter should broadly inclusive p...
187 2022-10-26 53.349998 elonmusk Entering Twitter let that sink D68z4K2wq7
188 2022-10-27 53.700001 elonmusk Dear Twitter Advertisers GMwHmInPAS
189 2022-10-28 53.700001 elonmusk Comedy now legal Twitter
This is the image im using for a mask这是我用于遮罩的图像
pil_im = Image.open('twitterLogo.png')
display(pil_im)
My mask我的面具
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)
So at this point im thinking its looking good, mask looks like it should, data looks like it should so the next step is creating the wordcloud:所以在这一点上我认为它看起来不错,面具看起来应该,数据看起来应该所以下一步是创建词云:
#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()
But instead of a nice twitter shaped cloud, I just get a rectangle, with the outline of the twitter logo pinned by little blue points within (kind of hard to see but if you look closely you can make out the shape of the twitter logo:但我得到的不是漂亮的 twitter 形状的云,而是一个矩形,其中 twitter 徽标的轮廓由小蓝点固定(有点难以看到,但如果你仔细观察,你可以辨认出 twitter 徽标的形状:
Ive tried using one or two other random png files as the mask with the same result.我尝试使用一个或两个其他随机 png 文件作为蒙版,结果相同。
Can somebody point out to me where im going wrong with this?有人可以指出我哪里出了问题吗?
Any help would be greatly appreciated.任何帮助将不胜感激。
As commented by Paul Brodersen the image used for the mask has to be black and white, with black corresponding to the area to be filled.正如 Paul Brodersen 所评论的那样,用于蒙版的图像必须是黑白的,黑色对应于要填充的区域。
Thanks Paul谢谢保罗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.