Python stylecloud creates cool word cloud diagrams

I participated in an innovation training this week, and after the end of the defense, I needed to make a ppt. In order to better display the content, I thought of using a word cloud chart. This article shares how to create a cool word cloud chart based on Python’s stylecloud. Stylecloud is an optimized and improved version of wordcloud. It is easy to operate and can be called directly.

  • The shape of the word cloud can be changed using free icons from Font Awesome;

  • Change the color palette to customize the style and change the background color via palettable;

  • Add gradients to make colors flow in a specific direction.

1. Basic instructions

First install the stylecloud and jieba libraries. Jieba is mainly used for semantic segmentation, because text parsing will be encountered when using it.

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple stylecloud``pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jieba

Default parameters for gen_stylecloud

def gen_stylecloud(text=None,` `file_path=None, # Input text/CSV file path` `size=512, # Size of stylecloud (length and width)` `icon_name='fas fa-flag\ ', # The icon name of the stylecloud shape (such as fas fa-grin). [default: fas fa-flag]` `palette='cartocolors.qualitative.Bold_5', # Palette (implemented through palettable). [default : cartocolors.qualitative.Bold_6]` `colors=None,` `background_color="white", # Background color` `max_font_size=200, # The maximum font size in stylecloud` `max_words=2000, # The maximum size that stylecloud can contain Number of words` `stopwords=True, # Boolean value, used to filter out common banned words` `custom_stopwords=STOPWORDS,` `icon_dir='.temp',` `output_name='stylecloud.png', # stylecloud Output text name` `gradient=None, # Gradient direction` `font_path=os.path.join(STATIC_PATH,``'Staatliches-Regular.ttf'), # Font used by stylecloud` `random_state=None, # Control Random states for words and colors` `collocations=True,` `invert_mask=False,` `pro_icon_path=None,` `pro_css_path=None):``

There is the following paragraph in word.txt

Python object-oriented --- the basic use of classes Object-oriented Class (class): It is a collection of objects used to describe the same attributes and methods. ``Class variables: Class variables are public throughout the instantiated object. Generally defined in the class and outside the function body. ``Method: Function in the class`` Data members: Class variables or instance variables are used to process data related to the class and its instance objects. ``Method rewriting: If the method inherited from the parent class cannot meet the needs of the subclass, it can be rewritten. This process is called method override, also known as method rewriting. ``Local variables: Variables defined in methods only affect the class of the current instance. ``Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. ``Inheritance: A derived class inherits the fields and methods of a base class. Inheritance also allows an object of a derived class to be treated as a base class object. Just like we define a fruit class, and then define a derived class apple, which has some attributes and methods of the fruit class, and also has some unique attributes and methods of its own, which are similar to the fruit class. It is an 'is-a' relationship. ``Instantiation: A specific object of a class. A class is like a template. Only after we instantiate it into an object can we perform corresponding operations on it. ``Object: An instance of a data structure defined by a class. Objects include two data members (class variables and instance variables) and methods.

By default, the word cloud is shaped like a flag

# -*- coding: utf-8 -*-`` ``import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r\ ', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Make Chinese Cloud words` `gen_stylecloud(text=result,` `font_path='C:\Windows\\Fonts\simhei.ttf',` `output_name='t1.png', ` `) # Must add Chinese font, otherwise the format will be wrong`` `` ``if __name__ == "__main__":` `file_name = './word.txt'` `cloud(file_name)``

2. Usage

2.1. Set background

# -*- coding: utf-8 -*-`` ``import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r\ ', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Make Chinese Cloud words` `gen_stylecloud(text=result,` `font_path='C:\Windows\\Fonts\simhei.ttf',` `background_color='black',` ` output_name='t2.png',` `) # Must add Chinese font, otherwise the format will be wrong`` `` ``if __name__ == "__main__":` `file_name = './word.txt\ '' `cloud(file_name)

2.2. Change the color palette to customize the style

# -*- coding: utf-8 -*-`` ``import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r\ ', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Make Chinese Cloud words` `gen_stylecloud(text=result,` `font_path='C:\Windows\\Fonts\simhei.ttf',` `palette='colorbrewer.diverging.Spectral_11\ ',` `output_name='t3.png',` `) # Must add Chinese font, otherwise the format will be wrong`` `` ``if __name__ == "__main__":` `file_name = './ word.txt'` `cloud(file_name)``

More color matching reference https://jiffyclub.github.io/palettable/

Change the shape of your word cloud using free icons from Font Awesome (no need to find shapes yourself)

https://fontawesome.dashgame.com/

Select an icon, right-click to copy, and paste the icon content into the code.

# -*- coding: utf-8 -*-`` ``import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r\ ', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Make Chinese Cloud words` `gen_stylecloud(text=result,` `font_path='C:\Windows\\Fonts\simhei.ttf',` `palette='cartocolors.diverging.Fall_4\ ',` `icon_name='fas fa-car',`` output_name='t4.png',` `) # Chinese font must be added, otherwise the format will be wrong`` `` ``if __name__ == \ "__main__":` `file_name = './word.txt'` `cloud(file_name)``

2.3. Set the color gradient direction

vertical direction

import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Create Chinese cloud words` `gen_stylecloud(text=result,` `font_path=' C:\Windows\Fonts\simhei.ttf',` `palette='cartocolors.diverging.TealRose_2',` `icon_name='fas fa-bell',` `gradient='vertical',` `output_name='t5.png',` `) # Chinese font must be added, otherwise the format will be wrong`` `` ``if __name__ == "__main__":` `file_name = './word.txt'` `cloud(file_name)

horizontal direction

import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):``with open(file_name, 'r', encoding='utf8') as f:` `word_list = jieba.cut(f.read())` `result = " ".join(word_list) # Separate word segments with ``# Create Chinese cloud words` `gen_stylecloud(text=result,` `font_path=' C:\Windows\Fonts\simhei.ttf',` `palette='cartocolors.diverging.TealRose_2',` `icon_name='fas fa-bell',` `gradient='horizontal',` `output_name='t6.png',` `) # Chinese font must be added, otherwise the format will be wrong`` `` ``if __name__ == "__main__":` `file_name = './word.txt'` `cloud(file_name)

3. Keyword mode

If you provide keywords to generate a word cloud, such as word1.txt

Python``ython``thon``hon``on``n``Matlab``atlab``tlab``lab``ab``b``Mathematical Analysis``Learning Analysis``Analysis`` Analyze `` big data `` data `` according to `` algorithm `` law `` artificial intelligence `` artificial intelligence `` intelligence `` innovation `` new `` entrepreneurship `` industry `` technology `` technology ` `I don’t know either. I don’t know. I don’t know. I know.

code show as below

import jieba``from stylecloud import gen_stylecloud`` `` ``def cloud(file_name):` `with open(file_name, 'r', encoding='utf8') as f:` `lines = f.readlines()` `result = ''` `for line in lines:` `result + = ' ' + line` `# Make Chinese cloud words` `gen_stylecloud(text=result,` `font_path ='C:\Windows\\Fonts\simhei.ttf',` `output_name='t10.png',` `) # Must add Chinese font, otherwise the format is wrong` ` `` ``if __name__ == "__main__":` `file_name = './word1.txt'` `cloud(file_name)

Readers can try other designs based on personalized needs.

Finally, share a URL to get the word cloud directly.

http://www.wordart.cc/


———————————END——————- ——–

Digression

In this era of big data, how can you keep up with scripting without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.


CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Essential development tools for Python

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template


CSDN spree:gift::[The most complete “Python learning materials” on the entire network are given away for free:free:! ](https://blog.csdn.net/weixin_68789096/article/details/132275547?spm=1001.2014.3001.5502)
(Safe link, click with confidence)

If there is any infringement, please contact us for deletion.