Comments (18)
@irn-bru @amueller
Here is the code for what @irn-bru is looking for:
def setListOfcolor_func(word=None, font_size=None,
position=None, orientation=None,
font_path=None, random_state=None):
#define the list of set colors
color_list = ["red", "#FFFFFF", "#000000", "skyblue", "yellow"]
#return a random color in the list
return np.random.choice(color_list)
# visulaize wordcloud
wordcloud = WordCloud(color_func=setListOfcolor_func).generate(YOUR_TEXT)
Thanks for writing such a helpful package @amueller
Cheers,
from word_cloud.
Look at the example here:
http://amueller.github.io/word_cloud/auto_examples/a_new_hope.html
And the references here:
http://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.WordCloud
You can either pass a color_func
to the constructor or recolor an existing word cloud.
The function must look like this:
def red_color_func(word, font_size, position, orientation, random_state=None):
return "hsl(10, 100%%, %d%%)" % random.randint(40, 100)
(this one will give you different lightnesses of a particular red hue)
from word_cloud.
Closing, let me know if you have additional questions.
from word_cloud.
from word_cloud.
Thanks for the reply, amueller.
Yes, that was the template I was working from and had successfully managed to adjust the colours like you described. What I wanted to do though, was be able to choose the exact individual colours (either via RGB, HEX values or another way) and your script would then use ONLY the colours I chose when rendering the final image instead of just applying a hue.
There is a word cloud generator over at http://www.wordle.net/create which lets you pick the colour(s) you wish to use and renders the image with those but the reason I prefer your script is because I can use a mask which for me is a must.
from word_cloud.
You can just define a list, and let the function return a random item of the list.
from word_cloud.
That sounds about right. Next question is how would I go about it?! (Sorry, I said I was new to Python)
from word_cloud.
Can you be more specific in your question?
from word_cloud.
Apologies, I was wondering what code I would use to define a list of specific colours I'd like the script to use. Like if I only wanted #FFFFFF and #000000 colours to be used in the text.
from word_cloud.
Not sure what color format PIL (which I use for drawing) accepts, but possibly just
np.random.choice(["red", "black"])
from word_cloud.
Thanks but I haven't a clue on how to get it working to test it. I added the 'import numpy as np' line and added 'np.random.choice(["red", "black"])' but nothing changed. This is in the 'a new hope' example script... am I right in thinking the code has to get added to there?!
Again, apologies for my severe lack of knowledge.
from word_cloud.
It could be interesting that wordcloud directly accepts combined structured data (pandas, dictionaries or whatever) including word+frequency+color all at once, without having to deal with custom functions.
word | frequency | color |
---|---|---|
USA | 25 | red |
China | 25 | yellow |
France | 8 | green |
Kenia | 8 | black |
Australia | 6 | blue |
Japan | 6 | yellow |
Brazil | 3 | red |
Morocco | 3 | black |
New Zealand | 3 | blue |
Portugal | 3 | green |
... | ... | ... |
(i.e. a long list of all world countries grouped by just 5 colors: i.e. the Olympic ring colors for each continent)
Pandas or database sources let you easily make joins to set colors depending on whatever criteria.
So I think there might be many similarly-structured datasets needing to use an approach like this.
It would be great to feed this directly to wordcloud.
Are there any documented examples already doing something similar?
Thanks @amueller and colaborators!
@abubelinha
from word_cloud.
@abubelinha what's the issue with the function, you can write a function that takes that dict, right?
from word_cloud.
@abubelinha what's the issue with the function, you can write a function that takes that dict, right?
"you can try" is probably more appropriate here (I have low programming skills).
I didn't say there was an issue. But it was a bit difficult for me to understand the example I found.
I will probably come up with a non-efficient solution (wasting memory, using unnecessary loops, whatever).
That's why I suggested it to be a good built-in feature, so people can quickly get using your library from pandas datasets.
That said, I am already working on it.
I started from generate_from_frequencies()
example and try to adapt it for dataframe-extracted frequencies.
Does generate_from_frequencies()
accept any other parameters than frequencies (i.e. to set colors) or do I need to apply them separately?
Thanks
from word_cloud.
OK, I ended up with something that works (at least with my first tests).
Probably not a good way of coding it, so I paste it here just in case somebody else can correct it or make suggestions.
@amueller I got specially confused about how to deal with random_state
and pass it to inner function calls
(I don't even know if that makes any sense).
The function should probably take many other parameters needed to configure wordcloud calls, so I would thank if anyone improves it as much as possible:
def df2wordcloud(df=None, colword=None, colfreq=None, colcolor='color', random_state=1, debug=False):
""" This takes a pandas dataframe which contains at least two columns (you must pass their names):
- one column for words,
- another for word frequencies
- and another (optional) for using fixed colors per word
- plus any other columns you need (i.e. to define -or not- other coloring functions)
If the 3rd column name is not passed, then default random colors are used instead.
Sorry about the odd debugging stuff. Please improve this code as much as you can:
- I wanted to count and output every color_function calls
(couldn't figure out how to make that counter without using global statements).
- colordict is created for nothing (I didn't know if I would need it for color_function)
- To-do: I couldn't figure out how to pass a random_state to this function and
propagate its value to inner function calls.
"""
from wordcloud import WordCloud,random_color_func
if df is None or colword is None or colfreq is None:
return
if debug:
# print("\nSOURCE DATAFRAME as a dict:\n\n", df.to_dict())
# print("\nSOURCE DATAFRAME as a list of lists:\n\n")
# print("pd.DataFrame(\n",df.values.tolist(),", \n columns = ",df.columns.tolist(), " )")
print("\nSOURCE DATAFRAME:\n\n", df)
wc = WordCloud(background_color="white", max_words=1000, scale=2, random_state=random_state)
freqdict = dict(zip(df[colword], df[colfreq].astype(int)))
if debug:
print("\nSOURCE FREQUENCES DICTIONARY (length={}):\n\n".format(len(freqdict)), freqdict)
if colcolor is not None:
colordict = dict(zip(df[colword], df[colcolor]))
if debug: print("\nSOURCE COLORS DICTIONARY (length={}):\n\n".format(len(colordict)), colordict)
if debug:
print("\n","- "*50)
wc.generate_from_frequencies(freqdict)
global counter
counter=0
if colcolor is not None: # use color function that takes color from dataframe color column
def my_color_func(word, font_size, position, orientation, font_path, random_state=1):
color = df[df[colword]==word][colcolor].item()
N = df[df[colword]==word][colfreq].item()
if debug:
global counter
counter+=1
print("{}. '{}' [N={}] to wordcloud with color --> '{}'".format(counter,word,N, color))
return(color)
wc.recolor(color_func=my_color_func)
else: # use default random colors
def my_color_func_default(word, font_size, position, orientation, font_path, random_state=1):
N = df[df[colword]==word][colfreq].item()
if debug:
global counter
counter+=1
print("{}. '{}' [N={}] to wordcloud with default color".format(counter,word,N))
return random_color_func(word, font_size, position, orientation, font_path, random_state)
wc.recolor(color_func=my_color_func_default)
pass
if debug:
print("\n --- I colored {} words out of {} --- \n".format(counter,len(df)))
import matplotlib.pyplot as plt
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
from word_cloud.
At this point, you can probably ask gpt-4 to write the code for you. The problem with creating a built-in function is that it would solve exactly your use-case, for a particular way of formatting the dataframe etc, while making the code for wordcloud even more complicated.
from word_cloud.
I don't understand what you mean with "particular way of formatting the dataframe" (unless you mean non-pandas dataframes).
The function I posted was not to solve my use-case. Actually, I haven't used it for anything yet.
I tried to design it to take any pandas dataframe. You must specify which columns contain the information for your wordcloud and which colours you want.
As the idea was somehow related to this issue title I just posted it here so anyone can reuse it (not expecting it to be part of this package).
It was more an exercise I did trying to understand how your library worked, so I could have a function for future usage (a python way to quickly create wordclouds from spreadsheet data with predefined colors).
Again, sorry about my dirty bad coding and debugging skills.
Anyway, it kinda worked but I couldn't figure out how to deal with random_state
and pass it to inner function calls.
Could you help? That and gpt-4 both go far beyond my understanding.
Thanks
from word_cloud.
I was commenting on this:
That's why I suggested it to be a good built-in feature, so people can quickly get using your library from pandas datasets.
from word_cloud.
Related Issues (20)
- MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 HOT 2
- Error in installing library wordcloud HOT 4
- ValueError: anchor not supported for multiline text HOT 3
- Maybe get rid of Cython HOT 4
- ValueError: Only supported for TrueType fonts on Databricks
- How to fix? : python setup.py egg_info did not run successfully. HOT 3
- Wordcloud not Support Multiple Languages in Once? Unicodes, Emoji, Glyphs all are not Works.
- Generating a wordcloud from a dictionary of terms with their frequencies HOT 1
- "AttributeError: 'ImageDraw' object has no attribute 'textbbox'" on Ubuntu 20.04 HOT 1
- test_cli_as_executable test failed on 1.9.3
- Works on Android Termux - quick tips
- Wordcloud returns an error when files exceed 15MB HOT 3
- Keeping together multi-word tokens
- clickable wordclouds
- Questions about Copilot + Open Source Software Hierarchy
- file `stopwords` is missing in the final wheel
- wordcloud not compatible with matplotlib 3.9.0 release on 16/05/2024 HOT 1
- generate_from_frequencies no longer working HOT 1
- Example links broken at https://pypi.org/project/wordcloud/
- I can't get "R&D" and "SMEs" in the same word cloud HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from word_cloud.