{"id":438,"date":"2023-02-13T12:14:10","date_gmt":"2023-02-13T12:14:10","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=438"},"modified":"2023-02-13T12:13:26","modified_gmt":"2023-02-13T12:13:26","slug":"plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/","title":{"rendered":"Plot data in pie (or) bar charts using Python ready-to-use function?"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-fc73e189\"><h5 class=\"uagb-heading-text\"><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How do plot data in pie (or) bar charts using Python ready-to-use function?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">Install the following module(s) if you haven&#8217;t installed them already:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install matplotlib<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">Ready-to-use Python function to plot data in a Pie (or) Bar chart:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def plot_chart(PlotValue, PlotLabel, chart_type, plot_heading='', PlotColor=[]):\n    # Plot Pie or Bar or Barh chart\n    plt.clf()\n    plot_color = True\n    \n    # set font size\n    # font = {'size': 9}\n    # plt.rc('font', **font)\n    \n    # No Data message\n    if len(PlotValue) &lt; 1: \n        messagebox.showinfo('Message', 'No Data to Plot!')\n        return\n    \n    if len(PlotValue) != len(PlotLabel):\n        messagebox.showinfo('Message', 'List size of Value and Label are not matching!')\n        return\n        \n    if len(PlotValue) != len(PlotColor):\n        if len(PlotColor) &lt; 1:\n            plot_color = False\n        else:\n            messagebox.showinfo('Message', 'List size of Value and Color are not matching!')\n            return\n    \n    # rename labels with PlotValue\n    for i in range(0, len(PlotLabel)):\n        PlotLabel[i] = str(PlotLabel[i]) + ' (' + str(PlotValue[i]) + ')'\n    \n    # Plot Pie Chart\n    if chart_type == 'pie' or chart_type == 'Pie' or chart_type == 'PIE':\n        if plot_color == True:\n            plt.pie(PlotValue, labels = PlotLabel, colors = PlotColor, shadow = True, startangle = 90, autopct='%1.1f%%', pctdistance=0.5, labeldistance=1.1, wedgeprops = {\"edgecolor\" : \"white\", 'linewidth': 0.5,'antialiased': True})\n        elif plot_color == False:\n            plt.pie(PlotValue, labels = PlotLabel, shadow = True, startangle = 90, autopct='%1.1f%%', pctdistance=0.5, labeldistance=1.1, wedgeprops = {\"edgecolor\" : \"white\", 'linewidth': 0.5,'antialiased': True})\n    \n    # Plot Vertical Bar Chart\n    if chart_type == 'bar' or chart_type == 'Bar' or chart_type == 'BAR':\n        if plot_color == True:\n            pps = plt.bar(PlotLabel, PlotValue, color = PlotColor)\n        elif plot_color == False:\n            pps = plt.bar(PlotLabel, PlotValue)\n    \n        for p in pps:\n            height = p.get_height()\n            width = p.get_width()\n            PerValue = round(height\/sum(PlotValue) * 100, 1)\n            plt.text(x=p.get_x() + width \/ 2, y=height+.10, s=\"{}%\".format(PerValue), ha='center')\n    \n    # Plot Horizantal Bar Chart\n    if chart_type == 'barh' or chart_type == 'Barh' or chart_type == 'BARH':\n        if plot_color == True:\n            pps = plt.barh(PlotLabel, PlotValue, color = PlotColor)\n        elif plot_color == False:\n            pps = plt.barh(PlotLabel, PlotValue)\n    \n        for p in pps:\n            width = p.get_height()\n            height = p.get_width()\n            PerValue = round(height\/sum(PlotValue) * 100, 1)\n            plt.text(y=p.get_y() + width \/ 2, x=height+.10, s=\"{}%\".format(PerValue), ha='left')\n    \n    plt.title(plot_heading)\n    plt.tight_layout()\n    plt.show()\n<\/pre>\n\n\n\n<p style=\"font-size:15px\">Write your main code as a sample below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\n\nplot_chart(&#91;43, 78], &#91;\"Not Found\", \"Downloaded\"], 'pie', \"Download Status\", &#91;\"#ff8c2d\", \"#47d147\"])\n\nplot_chart(&#91;43, 78], &#91;\"Not Found\", \"Downloaded\"], 'barh', \"Download Status\")<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">The output of the code is,<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_1.jpeg\" alt=\"\" class=\"wp-image-439\" width=\"480\" height=\"360\" srcset=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_1.jpeg 640w, https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_1-300x225.jpeg 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><figcaption class=\"wp-element-caption\">Pie Chart<\/figcaption><\/figure>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_2.jpeg\" alt=\"\" class=\"wp-image-440\" width=\"480\" height=\"360\" srcset=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_2.jpeg 640w, https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_2-300x225.jpeg 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><figcaption class=\"wp-element-caption\">Bar Chart<\/figcaption><\/figure>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-b8a3fde8\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How does the function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This code is a Python function to plot a chart using the <strong><code>Matplotlib <\/code><\/strong>library. The function takes in the following arguments:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <strong><code>PlotValue<\/code><\/strong>: A list of values to be plotted in the chart.<\/li>\n\n\n\n<li>The <strong><code>PlotLabel<\/code><\/strong>: A list of labels for each value to be plotted.<\/li>\n\n\n\n<li>The <strong><code>Chart_type<\/code><\/strong>: The type of chart to be plotted. The options are &#8216;pie&#8217;, &#8216;bar&#8217; or &#8216;barh&#8217; (vertical bar chart, or horizontal bar chart, respectively).<\/li>\n\n\n\n<li>The <strong><code>Plot_heading<\/code><\/strong> (Optional): The title of the chart.<\/li>\n\n\n\n<li>The <strong><code>PlotColor<\/code><\/strong> (Optional): A list of colors for each value to be plotted.<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:15px\">The function starts by clearing any existing plot on the figure. Then, it checks the length of the <strong><code>PlotValue<\/code><\/strong>, <strong><code>PlotLabel<\/code><\/strong>, and <strong><code>PlotColor <\/code><\/strong>lists to make sure that they have the same length. If the length of the <strong><code>PlotColor <\/code><\/strong>list is less than 1, the <strong><code>plot_color <\/code><\/strong>variable is set to <strong><code>False<\/code><\/strong>, indicating that colors won&#8217;t be used in the plot. If the lengths of the lists are not equal, a message box is shown with an error message.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>The function then renames the <strong><code>PlotLabel <\/code><\/strong>list by adding the corresponding <strong><code>PlotValue <\/code><\/strong>for each label.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Next, the function checks the value of the <strong><code>chart_type <\/code><\/strong>argument and plots the chart accordingly. If <strong><code>chart_type <\/code><\/strong>is <strong><code>pie<\/code><\/strong>, a pie chart is plotted using the PlotValue and PlotLabel lists, and the colors are set using the PlotColor list if plot_color is True.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>If <strong><code>chart_type <\/code><\/strong>is <strong><code>bar<\/code><\/strong> or <strong><code>barh<\/code><\/strong>, a bar or horizontal bar chart is plotted, respectively, using the PlotLabel and PlotValue lists. If plot_color is True, the colors are set using the PlotColor list. In both cases, the function adds the percentage values for each bar on the plot.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Finally, the function sets the title of the chart to the <strong><code>plot_heading <\/code><\/strong>argument and displays the chart using the <strong><code>plt.show()<\/code><\/strong> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install the following module(s) if you haven&#8217;t installed them already: Ready-to-use Python function to plot data in a Pie (or) Bar chart: Write your main code as a sample below, The output of the code is, This code is a Python function to plot a chart using the Matplotlib library. The function takes in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[27,25,26,24,14,13,16,15],"class_list":["post-438","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-bar-chart","tag-chart","tag-pie-chart","tag-plot","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Install the following module(s) if you haven&#8217;t installed them already: Ready-to-use Python function to plot data in a Pie (or) Bar chart: Write your main code as a sample below, The output of the code is, This code is a Python function to plot a chart using the Matplotlib library. The function takes in the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-13T12:14:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-13T12:13:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_1.jpeg\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/\",\"name\":\"Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-02-13T12:14:10+00:00\",\"dateModified\":\"2023-02-13T12:13:26+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Plot data in pie (or) bar charts using Python ready-to-use function?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/","og_locale":"en_US","og_type":"article","og_title":"Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com","og_description":"Install the following module(s) if you haven&#8217;t installed them already: Ready-to-use Python function to plot data in a Pie (or) Bar chart: Write your main code as a sample below, The output of the code is, This code is a Python function to plot a chart using the Matplotlib library. The function takes in the [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/","og_site_name":"tinyytopic.com","article_published_time":"2023-02-13T12:14:10+00:00","article_modified_time":"2023-02-13T12:13:26+00:00","og_image":[{"url":"https:\/\/tinyytopic.com\/wp-content\/uploads\/2023\/02\/Figure_1.jpeg"}],"author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/","name":"Plot data in pie (or) bar charts using Python ready-to-use function? - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-02-13T12:14:10+00:00","dateModified":"2023-02-13T12:13:26+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/plot-data-in-pie-or-bar-charts-using-python-ready-to-use-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"Plot data in pie (or) bar charts using Python ready-to-use function?"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":117,"uagb_excerpt":"Install the following module(s) if you haven&#8217;t installed them already: Ready-to-use Python function to plot data in a Pie (or) Bar chart: Write your main code as a sample below, The output of the code is, This code is a Python function to plot a chart using the Matplotlib library. The function takes in the&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=438"}],"version-history":[{"count":8,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/438\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/438\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}