{"id":74751,"date":"2026-09-20T03:01:00","date_gmt":"2026-09-19T17:01:00","guid":{"rendered":"https:\/\/www.rjmprogramming.com.au\/ITblog\/?p=74751"},"modified":"2026-09-19T22:25:52","modified_gmt":"2026-09-19T12:25:52","slug":"python-matplotlib-pie-chart-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/python-matplotlib-pie-chart-primer-tutorial\/","title":{"rendered":"Python Matplotlib Pie Chart Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=\"_blank\" href=\"http:\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.jpg\" rel=\"noopener\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Shower Songs Colour Coding Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.gif\" title=\"Pairings Game Tutorial\"  style=\"float:left;\"  \/><\/a><p class=\"wp-caption-text\">Python Matplotlib Pie Chart Primer Tutorial<\/p><\/div>\n<p>We&#8217;re off on a new discovery tour regarding &#8230;<\/p>\n<ul>\n<li>Python &#8230; as a programming language, and it&#8217;s module &#8230;<\/li>\n<li><a target=\"_blank\" title='Matplotlib: Visualization with Python' href='https:\/\/matplotlib.org\/' rel=\"noopener\">Matplotlib<\/a><\/li>\n<p> &#8230; starting down this road with a simple &#8230;<\/p>\n<ul>\n<li>command line Pie Chart application &#8230; Python <a target=\"_blank\" href=\"http:\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.py-GETME\" rel=\"noopener\">pie_chart_python.py<\/a> <font size=1>(superceeding <a target=\"_blank\" href=\"http:\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.py_GETME\" rel=\"noopener\">this<\/a>)<\/font> &#8230;<br \/>\n<code><br \/>\n# pie_chart_python.py<br \/>\n# September - 2026<br \/>\n# RJM Programming<br \/>\n# Thanks to https:\/\/www.w3schools.com\/python\/default.asp<br \/>\n<br \/>\nimport os<br \/>\nimport matplotlib<br \/>\n<br \/>\n# Set backend to Agg before importing pyplot<br \/>\nmatplotlib.use('Agg')<br \/>\n<br \/>\nimport matplotlib.pyplot as plt<br \/>\nimport matplotlib.image as mpimg<br \/>\n<br \/>\nimport numpy as np<br \/>\n#import plotly.express as px<br \/>\nfrom PIL import Image<br \/>\n<br \/>\nnums = []<br \/>\nsofar = 0<br \/>\n<br \/>\nif len(nums) &lt;= 0:<br \/>\n  while sofar &lt; 100:<br \/>\n     print(f\"Enter your percentage value regarding a pie chart where so far {sofar} has already been allocated.\")<br \/>\n     anum = input()<br \/>\n     nums.append(int(anum))<br \/>\n     sofar += int(anum)<br \/>\n<br \/>\ny = np.array(nums)<br \/>\n<br \/>\nfig, ax = plt.subplots()<br \/>\n<br \/>\nax.pie(y)<br \/>\n<br \/>\nfig.savefig(\"plot.png\")<br \/>\n<br \/>\n# 1. Load the image<br \/>\nimg = mpimg.imread('plot.png')<br \/>\n<br \/>\n# 2. Process or manipulate the image (Optional)<br \/>\n# For example, let's look at the shape or apply changes<br \/>\n#print(f\"Image shape: {img.shape}\")<br \/>\n<br \/>\n# 3. Create the plot without displaying it<br \/>\nplt.imshow(img)<br \/>\nplt.axis('off')  # Hide axis lines and labels<br \/>\n<br \/>\n# 4. Save the result to a file instead of showing it<br \/>\nplt.savefig('output_image.png', bbox_inches='tight', pad_inches=0)<br \/>\nplt.close()<br \/>\n<br \/>\n# Open the PNG image file<br \/>\n#with Image.open(\"plot.png\") as img:<br \/>\n    # Display the image using your system's default viewer<br \/>\n    #img.show()<br \/>\n    <br \/>\n    # Optional: Load the data into memory so you can safely close the file<br \/>\n    #img.load()<br \/>\n<br \/>\nexit()<br \/>\n<\/code><br \/>\n &#8230; and &#8230;\n<\/li>\n<li>working on our local Apache\/PHP <a target=\"_blank\" title='MAMP' href='http:\/\/www.mamp.info' rel=\"noopener\">MAMP<\/a> web server (only, so far, the reason being our public web server has no graphical $DISPLAY existant, and so the <i>plt.show()<\/i> Python codeline cannot be used, so we need to think more on integrations there) &#8230; PHP <a target=\"_blank\" href=\"http:\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.php_GETME\" rel=\"noopener\">pie_chart_python.php<\/a> &#8230;<br \/>\n<code><br \/>\n&lt;?php<br \/>\n\/\/ pie_chart_python.php<br \/>\n\/\/ RJM Programming - September, 2026<br \/>\nif (php_sapi_name() === 'cli') {<br \/>\n$command = '\/usr\/local\/bin\/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython.py'; \/\/ Your command<br \/>\nif (1 == 1) {<br \/>\nexec($command);<br \/>\n} else {<br \/>\n\/\/ Open the process pointer for reading<br \/>\n$handle = popen($command, 'r');<br \/>\n<br \/>\nif ($handle) {<br \/>\n    \/\/ Read and output the buffer until the end of the file<br \/>\n    while (!feof($handle)) {<br \/>\n        echo fread($handle, 2048);<br \/>\n        flush(); \/\/ Send it to the browser immediately<br \/>\n    }<br \/>\n    pclose($handle);<br \/>\n}<br \/>\n}<br \/>\nexit;<br \/>\n} else if (strpos(str_replace('?','',$_SERVER['QUERY_STRING']), '&') === false) {<br \/>\n$waspy=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.py');<br \/>\nif (strpos($waspy, \"nums = []\") !== false) {<br \/>\n    if (isset($_GET['array'])) {<br \/>\n      file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython.py', str_replace(\"nums = []\",\"nums = [\" . str_replace('+',' ',urldecode($_GET['array'])) . \"]\",$waspy));<br \/>\n    }<br \/>\n}<br \/>\n\/\/exec('php ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.php &gt; \/dev\/null 2&gt; \/dev\/null');<br \/>\nexec('cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; \/Applications\/MAMP\/bin\/php\/php7.4.33\/bin\/php -n .\/pie_chart_python.php &gt; \/dev\/null 2&gt; \/dev\/null ; open .\/plot.png');<br \/>\nsleep(3);<br \/>\n}<br \/>\necho \"&lt;html&gt;&lt;body style=\\\"background:url(.\/plot.png);background-repeat:no-repeat;background-size:contain;\\\"&gt;&lt;\/body&gt;&lt;\/html&gt;\";<br \/>\n?&gt;<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p> &#8230; allowing a web browser address bar URL such as &#8230;<\/p>\n<p><code><br \/>\nht<font color=black>tp<\/font>:\/\/localhost:8888\/pie_chart_python.php?array=12,5,1,68<br \/>\n<\/code><\/p>\n<p> &#8230; working for us, here, locally, displaying a Pie Chart &#8230;<\/p>\n<p><img decoding=\"async\" src=\"\/\/www.rjmprogramming.com.au\/Python\/pie_chart_python.jpg\"><\/img><\/p>\n<p> &#8230; via our local Python<sup>3<\/sup> Matplotlib modularized environment.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d74751' onclick='var dv=document.getElementById(\"d74751\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/python\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d74751' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re off on a new discovery tour regarding &#8230; Python &#8230; as a programming language, and it&#8217;s module &#8230; Matplotlib &#8230; starting down this road with a simple &#8230; command line Pie Chart application &#8230; Python pie_chart_python.py (superceeding this) &#8230; &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/python-matplotlib-pie-chart-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,17,19,29,37],"tags":[199,234,541,5657,802,932,944,2550,997,1012,5658,1319],"class_list":["post-74751","post","type-post","status-publish","format-standard","hentry","category-elearning","category-gui","category-installers","category-operating-system","category-tutorials","tag-chart","tag-command-line","tag-graphics","tag-matplotlib","tag-module","tag-php","tag-pie-chart","tag-pip","tag-programming","tag-python","tag-python3","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/74751"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=74751"}],"version-history":[{"count":5,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/74751\/revisions"}],"predecessor-version":[{"id":74757,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/74751\/revisions\/74757"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=74751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=74751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=74751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}