{"id":3201,"date":"2019-08-28T08:33:48","date_gmt":"2019-08-28T06:33:48","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3201"},"modified":"2019-08-30T17:29:44","modified_gmt":"2019-08-30T15:29:44","slug":"matplotlib-charts-1","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/matplotlib-charts-1\/","title":{"rendered":"Matplotlib charts 1"},"content":{"rendered":"<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3268\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4.png\" alt=\"\" width=\"1459\" height=\"393\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4.png 1459w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4-320x86.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4-768x207.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/charts4-960x259.png 960w\" sizes=\"auto, (max-width: 1459px) 100vw, 1459px\" \/><\/a>Let&#8217;s start a series of posts about <strong>matplotlib<\/strong>, a module to create charts and other <strong>visual<\/strong> data rappresentation tool.<\/p>\n<h2>Data on the screen<\/h2>\n<p>The first thing we are going to take a look at is the simplest chart ever with a sequence of data of my blog views. Here is the example from the matplot lib site.<\/p>\n<pre class=\"lang:default decode:true \">import numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nN = 5\r\nmenMeans = (20, 35, 30, 35, 27)\r\nwomenMeans = (25, 32, 34, 20, 25)\r\nmenStd = (2, 3, 4, 1, 2)\r\nwomenStd = (3, 5, 2, 3, 3)\r\nind = np.arange(N)    # the x locations for the groups\r\nwidth = 0.35       # the width of the bars: can also be len(x) sequence\r\n\r\np1 = plt.bar(ind, menMeans, width, yerr=menStd)\r\np2 = plt.bar(ind, womenMeans, width,\r\n             bottom=menMeans, yerr=womenStd)\r\n\r\nplt.ylabel('Scores')\r\nplt.title('Scores by group and gender')\r\nplt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))\r\nplt.yticks(np.arange(0, 81, 10))\r\nplt.legend((p1[0], p2[0]), ('Men', 'Women'))\r\n\r\nplt.show()<\/pre>\n<p>And this is the output<\/p>\n<h2><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-3203\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl001.png\" alt=\"\" width=\"336\" height=\"252\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl001.png 640w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl001-320x240.png 320w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/a><\/h2>\n<h2>Let&#8217;s transform this into the most simple chart ever<\/h2>\n<p>I want to get a basic chart with just data across months for an imaginary blog views. So, after some deletings and some changes we have this code:<\/p>\n<pre class=\"lang:default decode:true\">import numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nN = 10\r\nind = np.arange(N)    # the x locations for the groups\r\nviews = (634, 754, 937, 1300, 2200, 3000, 2800, 3600, 4200, 4600)\r\nwidth = 0.35       # the width of the bars: can also be len(x) sequence\r\np1 = plt.bar(ind, views, width)\r\nplt.ylabel('Views')\r\nplt.title('My blog views')\r\nplt.xticks(ind, ('N\\n18', 'D\\n18', 'G\\n19', 'F\\n19', 'M\\n19', 'A\\n19', 'M\\n19', 'J\\n19', 'J\\n19', 'A\\n19'))\r\nplt.yticks(np.arange(0, 81, 10))\r\nplt.show()<\/pre>\n<p>And this brought me to this chart<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3205\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b.png 640w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b-320x240.png 320w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>Now I thing that I miss is the number of views on the left&#8230; right?<\/p>\n<p>We need to adjust this<\/p>\n<pre class=\"lang:default decode:true\">plt.yticks(np.arange(0, 81, 10))<\/pre>\n<p>to this<\/p>\n<pre class=\"lang:default decode:true \">plt.yticks(np.arange(0, 5000, 1000))<\/pre>\n<p>The values on the <strong>vertical<\/strong> axe starts from 0 and ends at 5000 (because the max value is 4600) and the interval is set to 1.000.<\/p>\n<p>So, now the result is this:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3207\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b2.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b2.png 640w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mplb001b2-320x240.png 320w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<h2>In this video the live coding of the most basic chart ever<\/h2>\n<p>Let&#8217;s give a look at the <strong>live coding<\/strong> of a basic bar chart, even more basic than the one in the code above. It&#8217;s a short video, a little more than 6 minutes, to see how easy it easy to create charts with this very famouse tool: <strong>matplotlib<\/strong> for Python.<\/p>\n<p><iframe loading=\"lazy\" title=\"Draw bar charts with Matplotlib &amp; Python - 1\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/-6LhH7e7Fnw?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h2>What if I don&#8217;t like having two lists for views and months?<\/h2>\n<p>I do not like having to check what is the view corresponding to the mounth in different lists, so I decided to rearrange the code like this:<\/p>\n<pre class=\"lang:default decode:true\">import numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nviews = np.array([\r\n\t[634, \"N\", 2018],\r\n\t[754, \"D\", 2018],\r\n\t[937, \"G\", 2019],\r\n\t[1300, \"F\", 2019],\r\n\t[2200, \"M\", 2019],\r\n\t[3000, \"A\", 2019],\r\n\t[2800, \"M\", 2019],\r\n\t[3600, \"J\", 2019],\r\n\t[4200, \"J\", 2019],\r\n\t[4600, \"A\", 2019]\r\n])\r\nv= np.array([int(x[0]) for x in views])\r\nm = np.array([x[1] + \"\\n\" + str(x[2]) for x in views])\r\nind = np.arange(len(views))\r\np1 = plt.bar(ind, v, 0.35)\r\nplt.ylabel('Views')\r\nplt.title('My blog views')\r\nplt.xticks(ind, m)\r\nplt.yticks(np.arange(0, 5000, 1000))\r\nplt.show()\r\n<\/pre>\n<p>The result is just the same.<\/p>\n<p><strong>Other types of charts<\/strong><\/p>\n<p>This time we want to take advantage of 3 different ways of showing data. We are going to use a dictionary to store data. I used the same data from the code above (into an array) and changed it into a dictionary. As I cannot have the same key in a dictionary I made them different concatenating the month with the year and adding two letters to the month, so that there are no month with the same name and year.<\/p>\n<pre class=\"lang:default decode:true \">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nviews = np.array([\r\n\t[634, \"Nov\", 2018],\r\n\t[754, \"Dec\", 2018],\r\n\t[937, \"Gen\", 2019],\r\n\t[1300, \"Feb\", 2019],\r\n\t[2200, \"Mar\", 2019],\r\n\t[3000, \"Apr\", 2019],\r\n\t[2800, \"May\", 2019],\r\n\t[3600, \"Jun\", 2019],\r\n\t[4200, \"Jul\", 2019],\r\n\t[4600, \"Aug\", 2019]\r\n])\r\n\r\ndata = {}\r\nfor x in views:\r\n\tdata[x[1] + \"\\n\" + str(x[2][2:])] = int(x[0])\r\nprint(data)\r\n\r\nnames = list(data.keys())\r\nprint(names)\r\nvalues = list(data.values())\r\n\r\nfig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)\r\naxs[0].bar(names, values)\r\naxs[1].scatter(names, values)\r\naxs[2].plot(names, values)\r\nfig.suptitle('Categorical Plotting')\r\nplt.show()<\/pre>\n<p>The result will be this if you run the code:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3220\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png\" alt=\"\" width=\"1366\" height=\"671\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png 1366w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-320x157.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-768x377.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-960x472.png 960w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Now it&#8217;s up to you to choose the <strong>chart<\/strong> that better <strong>suits<\/strong> your data <strong>visualization<\/strong> aim. There is a clear raising of the views with a little &#8216;pause&#8217; in may that could be interesting to consider to <strong>understand<\/strong> <strong>the reason<\/strong> of that fact. I think the <strong>most effective<\/strong> of the graph to get different behaviours is the third, while in the first I can see more clearly the gap between the views in the month next to each other. In the <strong>first one<\/strong>, in fact, I can see more clearly that there is a bigger gap among frb 19 and mar 19 then from the other (the first 3 month grow is slower, while from the fourth the increase is much bigger). <strong>The second chart<\/strong> is perhaps the less useful for this kind of data of sequences according to a timeline. This chart is more useful to compare <strong>different entities<\/strong> (like in a <strong>market<\/strong> analysis with comparison of different <strong>brands<\/strong>, for example, just to make an example) with more dimentions to compare (that can be show with different colors of the dots and different dimensions of the dots).<\/p>\n<h2>Not using dictionaries<\/h2>\n<p>We can achieve the same result as above with this code:<\/p>\n<pre class=\"lang:default decode:true \">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nviews = np.array([\r\n\t[634, \"Nov\", 2018],\r\n\t[754, \"Dec\", 2018],\r\n\t[937, \"Gen\", 2019],\r\n\t[1300, \"Feb\", 2019],\r\n\t[2200, \"Mar\", 2019],\r\n\t[3000, \"Apr\", 2019],\r\n\t[2800, \"May\", 2019],\r\n\t[3600, \"Jun\", 2019],\r\n\t[4200, \"Jul\", 2019],\r\n\t[4600, \"Aug\", 2019]\r\n])\r\n\r\nvalues = [int(x[0]) for x in views]\r\nnames = [x[1] + \"\\n\" + x[2][2:] for x in views]\r\n\r\nfig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)\r\naxs[0].bar(names, values)\r\naxs[1].scatter(names, values)\r\naxs[2].plot(names, values)\r\nfig.suptitle('Categorical Plotting')\r\nplt.show()<\/pre>\n<p>The result being the same<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3220\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png\" alt=\"\" width=\"1366\" height=\"671\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003.png 1366w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-320x157.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-768x377.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/mpl003-960x472.png 960w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h2>Let&#8217;s experiment mixing different charts type<\/h2>\n<p>Why we do not <strong>merge<\/strong>\/<strong>blend<\/strong> two type of chart <strong>together<\/strong>? This is some <strong>code<\/strong> to do that.<\/p>\n<pre class=\"lang:default decode:true\">import matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nviews = np.array([\r\n\t[637, \"Nov\", 2018],\r\n\t[754, \"Dec\", 2018],\r\n\t[937, \"Gen\", 2019],\r\n\t[1267, \"Feb\", 2019],\r\n\t[2166, \"Mar\", 2019],\r\n\t[3019, \"Apr\", 2019],\r\n\t[2829, \"May\", 2019],\r\n\t[3643, \"Jun\", 2019],\r\n\t[4189, \"Jul\", 2019],\r\n\t[5019, \"Aug\", 2019]\r\n])\r\n# Data for x and y\r\nvalues = [int(x[0]) for x in views]\r\nnames = [x[1] + \"\\n\" + x[2][2:] for x in views]\r\nplt.plot(names, values)\r\n# This is the chart with bars\r\nind = np.arange(len(views))\r\nplt.bar(ind, values, 0.35)\r\nplt.show()<\/pre>\n<p>This is the output:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/plt005.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3229\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/plt005.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/plt005.png 640w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/plt005-320x240.png 320w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>Now we can have <strong>more<\/strong> visual <strong>informations<\/strong> about data at once. As we said previously, the most interesting charts for this data are the <strong>bar<\/strong> chart and the <strong>plot<\/strong> chart, now we&#8217;ve <strong>mixed<\/strong> them togheter, so we can have the best of both in one single chart, for a more intuitive <strong>visualization<\/strong> of the serie of data.<\/p>\n","protected":false},"excerpt":{"rendered":"How to create in few easy steps a simple bar chart with the matplotlib module\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/matplotlib-charts-1\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3261,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,324],"tags":[524,523,325,525,4,526],"class_list":["post-3201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-matplotlib","tag-bar","tag-chart","tag-matplotlib","tag-plot","tag-python","tag-scatter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3201"}],"version-history":[{"count":20,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3201\/revisions"}],"predecessor-version":[{"id":3285,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3201\/revisions\/3285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3261"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}