{"id":9342,"date":"2020-10-19T14:05:16","date_gmt":"2020-10-19T14:05:16","guid":{"rendered":"https:\/\/www.askpython.com\/?p=9342"},"modified":"2020-10-19T14:05:18","modified_gmt":"2020-10-19T14:05:18","slug":"plot-customize-pie-chart-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/plot-customize-pie-chart-in-python","title":{"rendered":"How to Plot and Customize a Pie Chart in Python?"},"content":{"rendered":"\n<p>A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. In a pie chart, the arc length of each slice is proportional to the quantity it represents. <\/p>\n\n\n\n<p>Pie charts are a popular way to represent the results of polls. In this tutorial, we will learn how to plot a pie-chart. Furthermore, we will learn how to customize a pie chart in python. <\/p>\n\n\n\n<p>Let&#8217;s get started. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create sample data<\/h2>\n\n\n\n<p>Let&#8217;s create some sample data that we can use while plotting the pie-chart.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n<\/pre><\/div>\n\n\n<p>The data is representative of an opinion poll on people&#8217;s preferred sport. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to plot a Pie Chart? <\/h2>\n\n\n\n<p>To plot a Pie-Chart we are going to use <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\n<\/pre><\/div>\n\n\n<p>To plot a basic Pie-chart we need the labels and the values associated with those labels. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nfig1, ax1 = plt.subplots()\nax1.pie(sizes, labels=labels)\nax1.axis(&#039;equal&#039;)  \nplt.show()\n<\/pre><\/div>\n\n\n<p>On running the above code snippet we 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=\"328\" height=\"243\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/pie-chat.png\" alt=\"Pie Chart\" class=\"wp-image-9344\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/pie-chat.png 328w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/pie-chat-300x222.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/pie-chat-160x120.png 160w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><figcaption>Pie Chart<\/figcaption><\/figure><\/div>\n\n\n\n<p>Note that this is a very basic pie-chart. <\/p>\n\n\n\n<p>Now let&#8217;s see how can we customize the pie-chart and make it look more interesting. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customizing a Pie Chart in Python<\/h2>\n\n\n\n<p>Matplotlib offers a lot of customization options when plotting a pie-chart. Let&#8217;s look at these, one by one. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Make a slice pop-out<\/h3>\n\n\n\n<p>You can make one or more slices of the pie-chart pop-out using the explode option. <\/p>\n\n\n\n<p>For this let&#8217;s declare an array that has the explosion values. The explosion array specifies the fraction of the radius with which to offset each slice.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nexplode = (0, 0.1, 0, 0)\nfig1, ax1 = plt.subplots()\nax1.pie(sizes, explode=explode, labels=labels)\nplt.show()\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"266\" height=\"242\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/explode.png\" alt=\"Explode\" class=\"wp-image-9345\"\/><figcaption>Explode<\/figcaption><\/figure><\/div>\n\n\n\n<p>Let&#8217;s try some different explosion values.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nexplode = (0.4, 0.2, 0.2, 0.2) \nfig1, ax1 = plt.subplots()\nax1.pie(sizes, explode=explode, labels=labels)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"239\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/explode-1.png\" alt=\"Explode \" class=\"wp-image-9350\"\/><figcaption>Explode <\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Rotate the Pie-chart <\/h3>\n\n\n\n<p>You can rotate the pie-chart by setting a <strong>strartangle<\/strong>. <\/p>\n\n\n\n<p>It rotates the start of the pie chart by specified value in degrees counterclockwise from the x-axis.<\/p>\n\n\n\n<p>Let&#8217;s see it in action :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nfig1, ax1 = plt.subplots()\nexplode = (0, 0.1, 0, 0) \nax1.pie(sizes, explode=explode, labels=labels,\n        shadow=True, startangle=90)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"286\" height=\"238\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/rotate.png\" alt=\"Rotate\" class=\"wp-image-9346\"\/><figcaption>Rotate 90<\/figcaption><\/figure><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; quick-code: true; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nfig1, ax1 = plt.subplots()\nexplode = (0, 0.1, 0, 0)\nax1.pie(sizes, explode=explode, labels=labels,\n        shadow=True, startangle=45)\n\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"235\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/rotate-1-1.png\" alt=\"Rotate \" class=\"wp-image-9352\"\/><figcaption>Rotate 45<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Display Percentages <\/h3>\n\n\n\n<p>You can also display the percentage along with each slice by 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\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\nfig1, ax1 = plt.subplots()\nexplode = (0, 0.1, 0, 0) \nax1.pie(sizes, explode=explode, labels=labels,autopct=&#039;%1.1f%%&#039;,\n        shadow=True, startangle=90)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"245\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/percentage.png\" alt=\"Percentage\" class=\"wp-image-9347\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/percentage.png 314w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/percentage-300x234.png 300w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><figcaption>Percentage<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Customizing colours <\/h3>\n\n\n\n<p>Matplotlib gives you the option to get creative and make your pie-chart look as vibrant as possible. <\/p>\n\n\n\n<p>To change the colours of your pie chart, use 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\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\ncolors = ( &quot;orange&quot;, &quot;cyan&quot;, &quot;brown&quot;, \n          &quot;grey&quot;) \nfig1, ax1 = plt.subplots()\nexplode = (0, 0.1, 0, 0) \nax1.pie(sizes, colors = colors, explode=explode, labels=labels,autopct=&#039;%1.1f%%&#039;,\n        shadow=True, startangle=90)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>Here we declare 4 colors that we want to use in a list. Then we pass that list as an argument while plotting the pie-chart. <\/p>\n\n\n\n<p>The output comes out as :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"287\" height=\"249\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/colour.png\" alt=\"Colour\" class=\"wp-image-9348\"\/><figcaption>Change Colours<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. Displaying colour codes<\/h3>\n\n\n\n<p>Along with your pie-chart, you can also display a box that contains the colour scheme of your pie-chart. This is especially useful when there are a lot of slices in your pie-chart. <\/p>\n\n\n\n<p>To display the colour codes use the following snippet :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n\nlabels = &#039;Cricket&#039;, &#039;Football&#039;, &#039;Hockey&#039;, &#039;F1&#039;\nsizes = &#x5B;15, 30, 45, 10]\n\ncolors = ( &quot;orange&quot;, &quot;cyan&quot;, &quot;brown&quot;, \n          &quot;grey&quot;) \nfig1, ax1 = plt.subplots()\nexplode = (0, 0.1, 0, 0) \n\nax1.pie(sizes, colors = colors, explode=explode, labels=labels,autopct=&#039;%1.1f%%&#039;, shadow=True, startangle=90)\npatches, texts, auto = ax1.pie(sizes, colors=colors, shadow=True, startangle=90,explode=explode, autopct=&#039;%1.1f%%&#039; )\n\nplt.legend(patches, labels, loc=&quot;best&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"265\" height=\"228\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/10\/colour-codes.png\" alt=\"Colour Codes\" class=\"wp-image-9349\"\/><figcaption>Colour Codes<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion <\/h3>\n\n\n\n<p>This tutorial was about how to plot and customize a pie-chart in Python using Matplotlib. Hope you had fun learning with us. To go through the official documentation of matplotlib, use this <a href=\"https:\/\/matplotlib.org\/3.1.1\/api\/_as_gen\/matplotlib.pyplot.pie.html?highlight=pie#matplotlib.pyplot.pie\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">link<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. In a pie chart, the arc length of each slice is proportional to the quantity it represents. Pie charts are a popular way to represent the results of polls. In this tutorial, we will learn how to plot [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":9353,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9342","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9342","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=9342"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/9342\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/9353"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=9342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=9342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=9342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}