guo-yong-zhi / WordCloud.jl

word cloud generator in julia
GNU General Public License v3.0
109 stars 2 forks source link

How to set the word colors to be based on weight? #15

Closed Kevin-Mattheus-Moerman closed 2 years ago

Kevin-Mattheus-Moerman commented 2 years ago

Thanks for this package. I am testing it at the moment and was not able to find out how to set word colors such that the color depends on the weight (based on occurrence). This could perhaps create a nice gradient of color rather than the random look I seem to get.

I've reproduced the code I'm using at the moment here:

using WordCloud

stopwords = WordCloud.stopwords_en
textfile = "/home/kevin/Desktop/temp.txt"
mydata = lowercase(read(textfile, String)) 

wc = wordcloud(
        processtext(mydata, stopwords=WordCloud.stopwords_en), 
        angles=(0, 90), 
        density=0.7,     
        mask=ellipse,   
        outline = 0,
        colors = :seaborn_dark,#colors = :Set1_5,
        fonts = "Tahoma",
        backgroundcolor="white",
        state=initwords!)
    placewords!(wc, style=:gathering, level=6)#, centeredword="modelling")
    pin(wc, "modelling") do # keep "modelling" in the center        
        generate!(wc, reposition=0.5) # don't teleport largest 30% words
        #generate!(wc)
    end

paint(wc, "/home/kevin/Desktop/plot.svg",background=true)

Currently I get something like: image

But I would prefer if it would look more like this: image

guo-yong-zhi commented 2 years ago

I'm sure we can make something similar, or even better, for example, this picture. To do that, you need a weight-ordered word list and a corresponding color list. If you pass a Vector to the colors argument, the item will be used one by one in cycle. So if you want the style of your second picture, you may need to set colors = [repeat(["gray"], 100); repeat(["red"], 100); repeat(["purple"], 100); repeat(["orange"], 100); repeat(["green"], 100)](suppose the length of word list is 500). Of course, you can set a more fine grain gradient colors (like my picture). You can refer to my approach.

Kevin-Mattheus-Moerman commented 2 years ago

That did the trick thanks!

plot10

guo-yong-zhi commented 2 years ago

I added a new function WordCloud.gradient to make it easy to generate gradients. Maybe I'll add an example sometime.