简体   繁体   中英

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.

# 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:

在此处输入图像描述

Ive tried using one or two other random png files as the mask with the same result.

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.

Thanks Paul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM