How to make Word Cloud characters monochromatic

background

In Python's Word Cloud Library, the text color is set randomly. You can set the color theme with colormap, but you cannot set the text to a single color.

Method

First, define a function that returns the text color. In the code below, it is set to white.

word_color_func = lambda *args, **kwargs: "white"

Next, pass the defined function to the argument (color_func) of the WordCloud class.

wordcloud = WordCloud(
    color_func = word_color_func,
    background_color = "black",
    width = 1000,
    height = 500,
).generate(text)

With the above, the text color of Word Cloud will be changed as shown in the figure below. The text information passed to WordCloud is Wikipedia's "Machine learning". wordcloud_example.png

Supplement

You can also set the font color in RGB format. Details can be found at here.

color_func=lambda *args, **kwargs: (255,255,255)

Recommended Posts

How to make Word Cloud characters monochromatic
Try to make Qiita's Word Cloud from your browser history
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
[Blender] How to make Blender scripts multilingual
How to make a crawler --Basic
How to make Selenium as light as possible
[Python] How to make a class iterable
[Cocos2d-x] How to make Script Binding (Part 2)
How to switch python versions in cloud9
How to make multi-boot USB (Windows 10 compatible)
How to make a Backtrader custom indicator
How to use GCP's Cloud Vision API
How to make a Pelican site map
[Cocos2d-x] How to make Script Binding (Part 1)
How to make a dialogue system dedicated to beginners
How to make an embedded Linux device driver (11)
How to make WTForms TextArea correspond to file drop
How to make an embedded Linux device driver (8)
How to make Spigot plugin (for Java beginners)
How to make an embedded Linux device driver (1)
How to make an embedded Linux device driver (4)
How to make multiple kernels selectable on Jupyter
How to make a dictionary with a hierarchical structure.
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (2)
If you want to create a Word Cloud.
How to make scrapy JSON output into Japanese
How to make an embedded Linux device driver (3)
How to call Cloud API from GCP Cloud Functions
How to use the Google Cloud Translation API
How to make a QGIS plugin (package generation)
How to remember when you forget a word
WEB scraping with python and try to make a word cloud from reviews
How to erase the characters output by Python
I read "How to make a hacking lab"
[Blender x Python] How to make an animation
How to make an embedded Linux device driver (6)
How to make Substance Painter Python plugin (Introduction)
[Blender x Python] How to make vertex animation
How to make an embedded Linux device driver (5)
[Python] How to handle Japanese characters with openCV
How to make an embedded Linux device driver (10)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
How to make Linux compatible with Japanese keyboard
How to make an embedded Linux device driver (9)
Inspired by "How to make pure functional JavaScript"
How to make AWS rekognition recognize local image files
[Continued] Inspired by "How to make pure functional JavaScript"
How to make Yubico Yubikey recognized in Manjaro Linux
Explain in detail how to make sounds with python
How to deploy a Django application on Alibaba Cloud
Scraping your Qiita articles to create a word cloud
How to make a shooting game with toio (Part 1)
How to make unit tests Part.2 Class design for tests
How to eliminate garbled characters in matplotlib output image