{"id":25781,"date":"2024-05-27T13:45:57","date_gmt":"2024-05-27T13:45:57","guid":{"rendered":"https:\/\/www.weetechsolution.com\/?p=25781"},"modified":"2024-05-29T05:03:26","modified_gmt":"2024-05-29T05:03:26","slug":"how-to-create-a-word-cloud-in-python-steps-with-codes","status":"publish","type":"post","link":"https:\/\/www.weetechsolution.com\/blog\/how-to-create-a-word-cloud-in-python-steps-with-codes\/","title":{"rendered":"How to Create a Word Cloud in Python &#8211; Steps with Codes"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"475\" src=\"https:\/\/www.weetechsolution.com\/wp-content\/uploads\/2024\/05\/How-to-Create-a-Word-Cloud-in-Python-Steps-with-Codes_1.jpg\" alt=\"How to Create a Word Cloud in Python - Steps with Codes\" class=\"wp-image-25839\" srcset=\"https:\/\/www.weetechsolution.com\/wp-content\/uploads\/2024\/05\/How-to-Create-a-Word-Cloud-in-Python-Steps-with-Codes_1.jpg 848w, https:\/\/www.weetechsolution.com\/wp-content\/uploads\/2024\/05\/How-to-Create-a-Word-Cloud-in-Python-Steps-with-Codes_1-768x430.jpg 768w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/figure>\n\n\n\n<p>Creating a word cloud is a fun and visually appealing way to highlight the most important terms in a dataset. Whether you&#8217;re analyzing text from social media, survey responses, or any large corpus, a word cloud can provide instant insights into the most frequent words. In this article, you will go through the process of building a word cloud in Python, a versatile language that\u2019s great for handling and visualizing data. It will cover everything from setting up your environment to customizing the appearance of your word cloud, ensuring you can tailor it to fit your specific needs.<\/p>\n\n\n\n<p>Word clouds are more than just pretty pictures, they\u2019re powerful tools for summarizing large amounts of text. They can quickly reveal trends and patterns that might not be immediately obvious. <strong><a href=\"\/blog\/pythons-emergence-in-the-world-of-video-game-design\" title=\"Python\u2019s Emergence in the World of Video Game Design\">Python<\/a><\/strong> has a rich ecosystem of libraries and makes it relatively straightforward to generate these visualizations. Whether you&#8217;re an expert programmer or just starting out as a fresher, you&#8217;ll find that creating a word cloud can be both educational and enjoyable. By the end of this guide, you will have all the skills you need to create your own word clouds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Install Required Libraries<\/strong><\/h3>\n\n\n\n<p>First, you&#8217;ll need to install the necessary libraries. You can do this using pip:<\/p>\n\n\n\n<p>In Bash<\/p>\n\n\n\n<p><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">pip install wordcloud matplotlib<\/mark><\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Import Libraries<\/strong><\/h3>\n\n\n\n<p>Next, import the necessary libraries in your Python script:<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><strong><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">from<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">wordcloud <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> WordCloud<\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> matplotlib.pyplot <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">as<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> plt<\/mark><\/code><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Prepare Text Data<\/strong><\/h3>\n\n\n\n<p>You need to have the text data ready. This can be any text you want to visualize, such as a document, an article, or even a string of text. For example:<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><strong><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">text =<\/mark><\/code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\"><code>\"\"\"<\/code><br><code>Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.<\/code><br><code>\"\"\"<\/code><\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Generate Word Cloud<\/strong><\/h3>\n\n\n\n<p>Use the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`WordCloud`<\/mark> class from the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`wordcloud`<\/mark> library to generate the word cloud. You can customize the word cloud with various parameters like <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`width`<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`height`<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`background_color`<\/mark>, etc.<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">wordcloud = WordCloud(width=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">800<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, height=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">400<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, background_color=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'white'<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">).generate(text)<\/mark><\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Display the Word Cloud<\/strong><\/h3>\n\n\n\n<p>Finally, use <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`matplotlib`<\/mark> to display the word cloud.<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><code>plt.figure(figsize=(<\/code><\/mark><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">10<\/mark><\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><code>, <\/code><\/mark><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">5<\/mark><\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><code>))<\/code><br><code>plt.imshow(wordcloud, interpolation=<\/code><\/mark><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'bilinear'<\/mark><\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><code>)<\/code><br><code>plt.axis(<\/code><\/mark><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'off'<\/mark><\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><code>)<\/code><\/mark><code># to remove the axis<\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">plt.show()<\/mark><\/code><\/strong><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Full Code<\/strong><\/h2>\n\n\n\n<p>Here\u2019s the complete code snippet to create a word cloud in Python:<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><code><strong># Import libraries<\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">from<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> wordcloud <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> WordCloud<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> matplotlib.pyplot <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">as<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> plt<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> <\/mark><\/strong><\/code><br><code><strong># Sample text<\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">text = <\/mark><\/strong><\/code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\"><code><strong>\"\"\"<\/strong><\/code><br><code><strong>Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.<\/strong><\/code><br><code><strong>\"\"\"<\/strong><\/code><\/mark><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> <\/mark><\/strong><\/code><br><code><strong># Generate word cloud<\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">wordcloud = WordCloud(width=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">800<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, height=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">400<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, background_color=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'white'<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">).generate(text)<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> <\/mark><\/strong><\/code><br><code><strong># Display the word cloud<\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">plt.figure(figsize=(<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">10<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">5<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">))<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">plt.imshow(wordcloud, interpolation=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'bilinear'<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">)<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">plt.axis(<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'off'<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">)<\/mark><\/strong><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">plt.show()<\/mark><\/strong><\/code><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Customization<\/strong><\/h2>\n\n\n\n<p>You can further customize your word cloud by using additional parameters. Here are some examples:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u273d<\/strong> <strong>Change Font<\/strong><\/h3>\n\n\n\n<p>Use a specific font by providing the path to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`.ttf`<\/mark> file.<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">wordcloud = WordCloud(font_path=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'path\/to\/font.ttf'<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">).generate(text)<\/mark><\/strong><\/code><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u273d<\/strong> <strong>Set Maximum Words<\/strong><\/h3>\n\n\n\n<p>Limit the number of words in the word cloud.<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">wordcloud = WordCloud(max_words=<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">200<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">).generate(text)<\/mark><\/strong><\/code><br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u273d <strong>Set Color Palette<\/strong><\/h3>\n\n\n\n<p>Use a specific color palette for the words.<\/p>\n\n\n\n<p>In Python<\/p>\n\n\n\n<p><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">from<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> wordcloud <\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> ImageColorGenerator<\/strong><\/mark><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> numpy <\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">as<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> np<\/strong><\/mark><\/code><br><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">from<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> PIL <\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">import<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> Image<\/strong><\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong> <\/strong><\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>mask = np.array(Image.<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">open<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>(<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">\"path\/to\/image.png\"<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>))<\/strong><\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>wordcloud = WordCloud(mask=mask, contour_width=<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ce5a77\" class=\"has-inline-color\">1<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>, contour_color=<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'black'<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>).generate(text)<\/strong><\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>image_colors = ImageColorGenerator(mask)<\/strong><\/mark><\/code><br><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation=<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#2aa075\" class=\"has-inline-color\">'bilinear'<\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"><strong>)<\/strong><\/mark><\/code><br><\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Creating a word cloud in Python is straightforward with the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">`wordcloud`<\/mark> library. By following the steps outlined above, you can visualize your text data effectively. Feel free to explore more customization options to make your word cloud unique and informative.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a word cloud is a fun and visually appealing way to highlight the most important terms in a dataset. Whether you&#8217;re analyzing text from social media, survey responses, or &#8230;<\/p>\n","protected":false},"author":2,"featured_media":25839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-25781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","category-web-development"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/posts\/25781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/comments?post=25781"}],"version-history":[{"count":17,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/posts\/25781\/revisions"}],"predecessor-version":[{"id":25846,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/posts\/25781\/revisions\/25846"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/media\/25839"}],"wp:attachment":[{"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/media?parent=25781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/categories?post=25781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weetechsolution.com\/wp-json\/wp\/v2\/tags?post=25781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}