{"id":9928,"date":"2020-10-26T17:43:43","date_gmt":"2020-10-26T17:43:43","guid":{"rendered":"https:\/\/www.askpython.com\/?p=9928"},"modified":"2020-10-26T17:43:45","modified_gmt":"2020-10-26T17:43:45","slug":"plot-a-treemap-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/plot-a-treemap-in-python","title":{"rendered":"How to Plot a Treemap in Python?"},"content":{"rendered":"\n<p>A treemap in Python is a visualization of data that splits a rectangle into sub-parts. The size of each subpart is in proportion to the data it represents. It is somewhat like a pie-chart. Although, treemaps can represent much-more complex data as compared to a pie-chart. <\/p>\n\n\n\n<p>It can help you visualize how single values compose a whole. <strong>Treemap<\/strong> charts also let you visualize hierarchical data using nested rectangles. <\/p>\n\n\n\n<p><strong>In this tutorial, we will learn how to plot treemaps in Python using the Squarify library in python. <\/strong><\/p>\n\n\n\n<p>Let&#8217;s start by installing Squarify.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install squarify\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"86\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/squarify.png\" alt=\"Squarify\" class=\"wp-image-9934\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/squarify.png 557w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/squarify-300x46.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><figcaption>Squarify<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Using Squarify to Plot a Treemap in Python<\/h2>\n\n\n\n<p>Once we have installed Squarify, we can start by importing it into our notebook. Let&#8217;s also import <a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\/python-matplotlib\" class=\"rank-math-link\">matplotlib<\/a>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport squarify \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">1. Plotting a basic treemap <\/h3>\n\n\n\n<p>To plot a very basic treemap, we just need the values for each rectangle. After plotting the treemap, the rectangles would be in proportion to these values. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport squarify \nsizes = &#x5B;40, 30, 5, 25]\nsquarify.plot(sizes)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/treemap.png\" alt=\"Treemap\" class=\"wp-image-9929\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/treemap.png 384w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/treemap-300x197.png 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><figcaption>Treemap<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Add labels to your treemap<\/h3>\n\n\n\n<p>You can add labels to the treemap in Python, using the following lines of code :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport squarify \nsizes=&#x5B;40, 30, 5, 25]\nlabel=&#x5B;&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;]\nsquarify.plot(sizes=sizes, label=label, alpha=0.6 )\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/lables.png\" alt=\"Lables\" class=\"wp-image-9930\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/lables.png 384w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/lables-300x197.png 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><figcaption>Labels<\/figcaption><\/figure><\/div>\n\n\n\n<p>If you run the same piece of code again, you will get the following output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/labels.png\" alt=\"Labels\" class=\"wp-image-9931\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/labels.png 384w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/labels-300x197.png 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/figure><\/div>\n\n\n\n<p>You can see that the color scheme of our treemap is different each time we run it.<em> The colors for rectangles are picked randomly<\/em>. Treemap also gives you the option to mention the colors along with sizes and labels. We will learn how to change the colors of a treemap next. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Change the colors in your treemap <\/h3>\n\n\n\n<p>To change the colors in your treemap in Python, make a list with the colors you want the treemap to have. Then pass that list to squarify.plot method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport squarify \nsizes=&#x5B;40, 30, 5, 25]\nlabel=&#x5B;&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;]\ncolor=&#x5B;&#039;red&#039;,&#039;blue&#039;,&#039;green&#039;,&#039;grey&#039;]\nsquarify.plot(sizes=sizes, label=label, color=color, alpha=0.6 )\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"384\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/changing-color.png\" alt=\"Changing Color\" class=\"wp-image-9932\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/changing-color.png 384w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/changing-color-300x197.png 300w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><figcaption>Changing Color<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Turn-off the plot axis <\/h3>\n\n\n\n<p>To plot the treemap without the plot-axis, use:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nplt.axis(&#039;off&#039;)\n<\/pre><\/div>\n\n\n<p>This line of code will turn off the plot axis. The complete code is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport squarify \nsizes=&#x5B;40, 30, 5, 25]\nlabel=&#x5B;&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;]\ncolor=&#x5B;&#039;red&#039;,&#039;blue&#039;,&#039;green&#039;,&#039;grey&#039;]\nsquarify.plot(sizes=sizes, label=label, color=color, alpha=0.6 )\nplt.axis(&#039;off&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"231\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/plot-axis-off.png\" alt=\"Plot Axis Off\" class=\"wp-image-9933\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/plot-axis-off.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/plot-axis-off-300x199.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><figcaption>Plot Axis Off<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Plot treemap for a Dataset <\/h2>\n\n\n\n<p>In this part of the tutorial, we will learn how to plot a treemap for a dataset. We are going to use the titanic dataset. Let&#8217;s start by importing the dataset. To simplify the process of importing the dataset we are going to use the <strong>seaborn<\/strong> library. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Importing the dataset <\/h3>\n\n\n\n<p>To import the titanic dataset from the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-seaborn-tutorial\" class=\"rank-math-link\">seaborn library<\/a> into your Python notebook, use:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport seaborn as sns\ntitanic = sns.load_dataset(&#039;titanic&#039;)\ntitanic.head()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"205\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-dataset.png\" alt=\"Titanic Dataset\" class=\"wp-image-9935\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-dataset.png 1025w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-dataset-300x60.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-dataset-768x154.png 768w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><figcaption>Titanic Dataset<\/figcaption><\/figure><\/div>\n\n\n\n<p>The dataset contains information about the passengers of Titanic. <\/p>\n\n\n\n<p>We want to plot a treemap for the people who survived according to the class they were travelling in. <\/p>\n\n\n\n<p>The data in its original format is not ready for plotting a treemap. We will carry out some manipulations and try to extract data that we can use to plot a treemap. <\/p>\n\n\n\n<p>To get the survivors for each class we are going to use <strong>group by method<\/strong> on our data. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Preparing the Data for Plotting<\/h3>\n\n\n\n<p>You can use the groupby function on the dataset as shown below :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = titanic.groupby(&#039;class&#039;)&#x5B;&#x5B;&#039;survived&#039;]].sum()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"172\" height=\"171\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby.png\" alt=\"Groupby\" class=\"wp-image-9936\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby.png 172w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby-150x150.png 150w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby-120x120.png 120w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby-24x24.png 24w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby-48x48.png 48w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/groupby-96x96.png 96w\" sizes=\"auto, (max-width: 172px) 100vw, 172px\" \/><figcaption>Groupby<\/figcaption><\/figure><\/div>\n\n\n\n<p>This gives us the sum of total survivors grouped according to the class.<\/p>\n\n\n\n<p>Now we need to extract the data and labels as <a href=\"https:\/\/www.askpython.com\/python\/difference-between-python-list-vs-array\" class=\"rank-math-link\">lists<\/a> from this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = titanic.groupby(&#039;class&#039;)&#x5B;&#x5B;&#039;survived&#039;]].sum().index.get_level_values(0).tolist()\nprint(a)\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;First&#039;, &#039;Second&#039;, &#039;Third&#039;]\n<\/pre><\/div>\n\n\n<p>This gives us the labels in the form of a list. To get the values corresponding to these labels, use :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = titanic.groupby(&#039;class&#039;)&#x5B;&#x5B;&#039;survived&#039;]].sum().reset_index().survived.values.tolist()\nprint(d)\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;136, 87, 119]\n<\/pre><\/div>\n\n\n<p>Now we have the labels and data as lists. We can use these to plot a treemap.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Plot the treemap <\/h3>\n\n\n\n<p>To plot the treemap, use the following line of code : <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsquarify.plot(sizes=d, label=a, alpha=.8)\nplt.axis(&#039;off&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"231\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-treemap.png\" alt=\"Titanic Treemap\" class=\"wp-image-9937\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-treemap.png 349w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/titanic-treemap-300x199.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><figcaption>Titanic Treemap<\/figcaption><\/figure><\/div>\n\n\n\n<p>Visualizing the treemap, we can get a rough idea about the number of survivors in the first, second, and third class. Just by looking at the treemap, we can confidently say that the second class has the least number of survivors. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Complete code to plot a treemap in Python<\/h3>\n\n\n\n<p>The complete code from this section is given below :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport seaborn as sns\nimport squarify \nimport matplotlib.pyplot as plt\n\ntitanic = sns.load_dataset(&#039;titanic&#039;)\n\na = titanic.groupby(&#039;class&#039;)&#x5B;&#x5B;&#039;survived&#039;]].sum().index.get_level_values(0).tolist()\n\nd = titanic.groupby(&#039;class&#039;)&#x5B;&#x5B;&#039;survived&#039;]].sum().reset_index().survived.values.tolist()\n\nsquarify.plot(sizes=d,label=a, alpha=.8 )\nplt.axis(&#039;off&#039;)\nplt.show()\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion <\/h2>\n\n\n\n<p>In this tutorial, we learned how to plot a treemap in python using Squarify. Hope you had fun learning with us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A treemap in Python is a visualization of data that splits a rectangle into sub-parts. The size of each subpart is in proportion to the data it represents. It is somewhat like a pie-chart. Although, treemaps can represent much-more complex data as compared to a pie-chart. It can help you visualize how single values compose [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":9938,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=9928"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/9938"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=9928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=9928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=9928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}