{"id":2276,"date":"2024-07-23T12:44:30","date_gmt":"2024-07-23T03:44:30","guid":{"rendered":"https:\/\/secondlife.lol\/?p=2276"},"modified":"2024-08-16T13:22:27","modified_gmt":"2024-08-16T04:22:27","slug":"treemap-visualization-gni-python","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/en\/treemap-visualization-gni-python\/","title":{"rendered":"Creating Treemaps with Python (feat. GNI Data Analytics)"},"content":{"rendered":"<p>Many people like to use data visualizations to intuitively understand complex information, and treemaps are a great tool to do just that. <\/p>\n\n\n<style>.kb-image2276_beb32c-86 .kb-image-has-overlay:after{opacity:0.3;}<\/style>\n<div class=\"wp-block-kadence-image kb-image2276_beb32c-86\"><figure class=\"aligncenter size-medium_large\"><img decoding=\"async\" width=\"768\" height=\"575\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-18-768x575.jpg\" alt=\"\ud30c\uc774\uc36c \uc774\uc6a9\ud55c \ud2b8\ub9ac\ub9f5 \uadf8\ub9bc\" class=\"kb-img wp-image-2358\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-18-768x575.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-18-300x225.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-18-600x449.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-18.jpg 1200w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<p>In this post, I'll show you how to create a treemap in Python and use it to analyze the Gross National Income (GNI) of countries in 2014.<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<h2 class=\"wp-block-heading\">What is a treemap?<\/h2>\n\n\n\n<p>A treemap is a way to visualize hierarchical data in a rectangular shape. Each <strong>Size of the rectangle<\/strong>represents the value of that data, and <strong>Other colors<\/strong>to separate groups.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python code description<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Install and load the required packages<\/h3>\n\n\n\n<p>In Python, the <code>pandas<\/code>, <code>matplotlib<\/code>, <code>squarify<\/code>and <code>rpy2<\/code> package. First, install and load the required packages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import font_manager, rc\nimport squarify\nimport rpy2.robjects as robjects\nfrom rpy2.robjects import pandas2ri\n\n# Setting up a Korean font on MacOS\nfont_path = '\/Library\/Fonts\/Arial Unicode.ttf' # Change to the actual font file path\nfont = font_manager.FontProperties(fname=font_path).get_name()\nrc('font', family=font)\n\n# Install the required packages (if they need to be installed)\n# !pip install pandas matplotlib squarify rpy2\n\n# Install and load R packages using Rpy2\nrobjects.r('install.packages(\"treemap\", repos=\"http:\/\/cran.us.r-project.org\")')\nrobjects.r('library(treemap)')<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pandas: A library that provides high-performance data structures and data analysis tools for data analysis in Python. DataFrame objects make it easy to manipulate and analyze data.<br><\/li>\n\n\n\n<li>Matplotlib: A library that allows you to visualize data in Python in various forms. You can create plots, graphs, histograms, and more.<br><\/li>\n\n\n\n<li>Squarify: A Python library that generates treemaps. It allows you to visualize the relative size of your data in rectangular form.<\/li>\n\n\n\n<li>Rpy2: An interface between R and Python that allows you to import datasets from R into Python.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Import and preprocess data<\/h3>\n\n\n\n<p><code>rpy2<\/code>using the <a href=\"https:\/\/cran.r-project.org\/package=treemap\" data-type=\"link\" data-id=\"https:\/\/cran.r-project.org\/package=treemap\" target=\"_blank\" rel=\"noopener\">R's <code>treemap<\/code> Packages<\/a>to load and preprocess the GNI2014 dataset provided by the GNI. I didn't find anything in the Python package that I could use, so I borrowed the data from the R package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Converting # R data to pandas DataFrame\npandas2ri.activate()\n\nLoad GNI2014 dataset from treemap package in # R\nrobjects.r('data(\"GNI2014\")')\nGNI2014 = robjects.r('as.data.frame(GNI2014)')\nGNI2014 = pandas2ri.rpy2py(GNI2014)\n\nAdd labels to the top # and top 25% GNI countries\nGNI2014['label'] = GNI2014.apply(lambda x: x['country'] if x['GNI'] &gt; GNI2014['GNI'].quantile(0.75) else '', axis=1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. create a treemap<\/h3>\n\n\n\n<p>Now, the <code>squarify<\/code>and <code>matplotlib<\/code>to generate a treemap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Create a # Treemap\nplt.figure(figsize=(12, 8))\ncolors = plt.cm.viridis(GNI2014['GNI'] \/ max(GNI2014['GNI']))\nsquarify.plot(sizes=GNI2014['GNI'], label=GNI2014['label'], color=colors, alpha=.8)\nplt.axis('off')\nplt.title('GNI 2014 dataset treemap', fontsize=20)\nplt.suptitle('Gross National Income (GNI) comparison by country', fontsize=16)\nplt.show()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Interpreting the results<\/h2>\n\n\n\n<p>When you run the code above, you'll get a treemap that allows you to intuitively compare the GNI of different countries, as shown at the top of the post. The size of each rectangle represents the GNI of that country, and the color depends on the GNI value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>What can I do with Pandas? <br>- Pandas uses dataframes to read, write, manipulate, and analyze data. You can easily do things like import a CSV file or select specific columns of data.<\/li>\n\n\n\n<li>What is the difference between Matplotlib and other visualization libraries?<br>- Matplotlib is very flexible and can create many different types of plots, but it can be complicated to use for beginners. Seaborn builds on Matplotlib to provide simpler visualizations, and Plotly makes it easy to create interactive graphs.<\/li>\n\n\n\n<li>When is Squarify useful?<br>- Squarify helps you create treemaps. Treemaps help you compare data intuitively, and are especially useful for visually clarifying hierarchical data or comparisons.<\/li>\n\n\n\n<li>What does the Treemap library provide?<br>- The Treemap library provides datasets for treemap visualization and helps you practice with example data. Here, we used the load_gni2014 function to load 2014 GNI data.<\/li>\n\n\n\n<li>How do I set a different color for each rectangle when creating a treemap?<br>- You can use Squarify and Matplotlib together to set colors. You can use a colormap like plt.cm.viridis to color based on data values.<\/li>\n\n\n\n<li>What does the apply function do in data preprocessing?<br>- The apply function allows you to apply a function to each row or column of a dataframe. Here it was used to add the country name as a label if each country's GNI was in the top 25%.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Full code and finalization<\/h2>\n\n\n\n<p>Finally, let's clean up the entire code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import font_manager, rc\nimport squarify\nimport rpy2.robjects as robjects\nfrom rpy2.robjects import pandas2ri\n\n# Setting up a Korean font on MacOS\nfont_path = '\/Library\/Fonts\/Arial Unicode.ttf' # Change to the actual font file path\nfont = font_manager.FontProperties(fname=font_path).get_name()\nrc('font', family=font)\n\n# Install the necessary packages (if they need to be installed)\n# !pip install pandas matplotlib squarify rpy2\n\n# Install and load R packages using Rpy2\nrobjects.r('install.packages(\"treemap\", repos=\"http:\/\/cran.us.r-project.org\")')\nrobjects.r('library(treemap)')\n\nConvert # R data to pandas DataFrame\npandas2ri.activate()\n\nLoad the GNI2014 dataset from # R's treemap package\nrobjects.r('data(\"GNI2014\")')\nGNI2014 = robjects.r('as.data.frame(GNI2014)')\nGNI2014 = pandas2ri.rpy2py(GNI2014)\n\nAdd labels to the top # and top 25% GNI countries\nGNI2014['label'] = GNI2014.apply(lambda x: x['country'] if x['GNI'] &gt; GNI2014['GNI'].quantile(0.75) else '', axis=1)\n\nGenerate a # treemap\nplt.figure(figsize=(12, 8))\ncolors = plt.cm.viridis(GNI2014['GNI'] \/ max(GNI2014['GNI']))\nsquarify.plot(sizes=GNI2014['GNI'], label=GNI2014['label'], color=colors, alpha=.8)\nplt.axis('off')\nplt.title('GNI 2014 dataset treemap', fontsize=20)\nplt.suptitle('Gross National Income (GNI) comparison by country', fontsize=16)\nplt.show()\n<\/code><\/pre>\n\n\n\n<p>In this post, you learned how to create a treemap using Python. With these visualization techniques, you can analyze and communicate your data more efficiently. Explore more data visualization techniques in the future!<\/p>\n\n\n\n<p>If you'd like to see the process and results of the same visualization using R instead of Python, you can check out the <a href=\"https:\/\/secondlife.lol\/en\/r-treemap-visualization-gni2014\/\" data-type=\"post\" data-id=\"2239\">Visualizing treemaps in R: Comparing Gross National Income (GNI) across countries using the GNI 2014 dataset<\/a> Check out the post.<\/p>","protected":false},"excerpt":{"rendered":"<p>Many people use data visualization to intuitively understand complex information...<\/p>","protected":false},"author":3,"featured_media":2363,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3],"tags":[181,162,34,182,164,175],"class_list":["post-2276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-coding","tag-gni","tag-matplotlib","tag-python","tag-squarify","tag-164","tag-175"],"taxonomy_info":{"category":[{"value":3,"label":"\ud30c\uc774\uc36c(Python)"}],"post_tag":[{"value":181,"label":"GNI"},{"value":162,"label":"Matplotlib"},{"value":34,"label":"python"},{"value":182,"label":"squarify"},{"value":164,"label":"\ub370\uc774\ud130 \uc2dc\uac01\ud654"},{"value":175,"label":"\ud2b8\ub9ac\ub9f5"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/\ud30c\uc774\uc36c-\uc139\uc158-\uc0bc\uac01\ud568\uc218-\uadf8\ub798\ud504-\uadf8\ub9ac\uae30-\uc378\ub124\uc77c-600x600.jpg",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/en\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":3,"name":"\ud30c\uc774\uc36c(Python)","slug":"python-coding","term_group":0,"term_taxonomy_id":3,"taxonomy":"category","description":"","parent":20,"count":116,"filter":"raw","cat_ID":3,"category_count":116,"category_description":"","cat_name":"\ud30c\uc774\uc36c(Python)","category_nicename":"python-coding","category_parent":20}],"tag_info":[{"term_id":181,"name":"GNI","slug":"gni","term_group":0,"term_taxonomy_id":181,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":162,"name":"Matplotlib","slug":"matplotlib","term_group":0,"term_taxonomy_id":162,"taxonomy":"post_tag","description":"","parent":0,"count":19,"filter":"raw"},{"term_id":34,"name":"python","slug":"python","term_group":0,"term_taxonomy_id":34,"taxonomy":"post_tag","description":"","parent":0,"count":21,"filter":"raw"},{"term_id":182,"name":"squarify","slug":"squarify","term_group":0,"term_taxonomy_id":182,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":164,"name":"\ub370\uc774\ud130 \uc2dc\uac01\ud654","slug":"%eb%8d%b0%ec%9d%b4%ed%84%b0-%ec%8b%9c%ea%b0%81%ed%99%94","term_group":0,"term_taxonomy_id":164,"taxonomy":"post_tag","description":"","parent":0,"count":52,"filter":"raw"},{"term_id":175,"name":"\ud2b8\ub9ac\ub9f5","slug":"%ed%8a%b8%eb%a6%ac%eb%a7%b5","term_group":0,"term_taxonomy_id":175,"taxonomy":"post_tag","description":"","parent":0,"count":2,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/comments?post=2276"}],"version-history":[{"count":7,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2276\/revisions"}],"predecessor-version":[{"id":2374,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2276\/revisions\/2374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media\/2363"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media?parent=2276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/categories?post=2276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/tags?post=2276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}