{"id":6436,"date":"2017-05-04T08:11:41","date_gmt":"2017-05-04T12:11:41","guid":{"rendered":"http:\/\/pzd.hmy.temporary.site\/?p=6436"},"modified":"2017-05-04T08:11:41","modified_gmt":"2017-05-04T12:11:41","slug":"creating-graphs-with-python-and-goopycharts","status":"publish","type":"post","link":"https:\/\/datascienceplus.com\/creating-graphs-with-python-and-goopycharts\/","title":{"rendered":"Creating Graphs with Python and GooPyCharts"},"content":{"rendered":"<p>Last summer, I came across an interesting plotting library called <a href=\"https:\/\/github.com\/Dfenestrator\/GooPyCharts\" target=\"_blank\">GooPyCharts<\/a> which is a Python wrapper for the Google Charts API. In this article, we will spend a few minutes learning how to use this interesting package. GooPyCharts follows syntax that is similar to MATLAB and is actually meant to be an alternative to matplotlib.<\/p>\n<p>To install GooPyCharts, all you need to do is use pip like this:<\/p>\n<pre>pip install gpcharts<\/pre>\n<p>Now that we have it installed, we can give it a whirl!<\/p>\n<h2>Our First Graph<\/h2>\n<p>Using GooPyCharts to create a chart or graph is extremely easy. In fact, you can create a simple graph in 3 lines of code:<\/p>\n<pre>\r\n&gt;&gt;&gt; from gpcharts import figure\r\n&gt;&gt;&gt; my_plot = figure(title='Demo')\r\n&gt;&gt;&gt; my_plot.plot([1, 2, 10, 15, 12, 23])<\/pre>\n<p>If you run this code, you should see your default browser pop open with the following image displayed:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-490x341.png\" alt=\"\" width=\"490\" height=\"341\" class=\"alignnone size-medium wp-image-6438\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-490x341.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-768x534.png 768w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-600x417.png 600w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-400x277.png 400w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple-300x209.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/gpchart_simple.png 920w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>You will note that you can download the figure as a PNG or save the data that made the chart as a CSV file. GooPyCharts also integrates with the Jupyter Notebook.<\/p>\n<h2>Creating a Bar Graph<\/h2>\n<p>The GooPyCharts package has a nice <strong>testGraph.py<\/strong> script included to help you learn how to use the package. Unfortunately it doesn&#8217;t actually demonstrate different types of charts. So I took one of the examples from there and modified it to create a bar chart:<\/p>\n<pre>\r\nfrom gpcharts import figure\r\n\r\nfig3 = figure()\r\nxVals = ['Temps','2016-03-20','2016-03-21','2016-03-25','2016-04-01']\r\nyVals = [['Shakuras','Korhal','Aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]]\r\n\r\nfig3.title = 'Weather over Days'\r\nfig3.ylabel = 'Dates'\r\nfig3.bar(xVals, yVals)<\/pre>\n<p>You will note that in this example we create our title using the figure instance&#8217;s <strong>title<\/strong> property. We also set the <strong>ylabel<\/strong> the same way. You can also see how to define dates for the chart as well as set an automatic legend using nested lists. Finally you can see that instead of calling <strong>plot<\/strong> we need to call <strong>bar<\/strong> to generate a bar chart. <\/p>\n<p>Here is the result:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather-490x294.png\" alt=\"\" width=\"490\" height=\"294\" class=\"alignnone size-medium wp-image-6439\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather-490x294.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather-768x461.png 768w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather-600x360.png 600w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather-300x180.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/weather.png 1000w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<h2>Creating Other Types of Graphs<\/h2>\n<p>Let&#8217;s modify the code a bit more and see if we can create other types of graphs. We will start with a scatter plot:<\/p>\n<pre>from gpcharts import figure\r\n\r\nmy_fig = figure()\r\nxVals = ['Dates','2016-03-20','2016-03-21','2016-03-25','2016-04-01']\r\nyVals = [['Shakuras','Korhal','Aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]]\r\n\r\nmy_fig.title = 'Scatter Plot'\r\nmy_fig.ylabel = 'Temps'\r\n\r\nmy_fig.scatter(xVals, yVals)<\/pre>\n<p>Here we can most of the same data that we used in the last example. We just need to modify a few values to make the X and Y labels work correctly and we need to title the graph with something that makes sense. <\/p>\n<p>When you run this code, you should see something like this:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter-490x294.png\" alt=\"\" width=\"490\" height=\"294\" class=\"alignnone size-medium wp-image-6440\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter-490x294.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter-768x461.png 768w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter-600x360.png 600w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter-300x180.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/scatter.png 1000w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>That was pretty simple. Let&#8217;s try creating a quick and dirty histogram:<\/p>\n<pre>from gpcharts import figure\r\n\r\nmy_fig = figure()\r\nmy_fig.title = 'Random Histrogram'\r\nmy_fig.xlabel = 'Random Values'\r\nvals = [10, 40, 30, 50, 80, 100, 65]\r\nmy_fig.hist(vals)<\/pre>\n<p>The histogram is much simpler than the last two charts we created as it only needs one list of values to create it successfully. <\/p>\n<p>This is what I got when I ran the code:<br \/>\n<a href=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram-490x294.png\" alt=\"\" width=\"490\" height=\"294\" class=\"alignnone size-medium wp-image-6441\" srcset=\"https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram-490x294.png 490w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram-768x461.png 768w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram-600x360.png 600w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram-300x180.png 300w, https:\/\/datascienceplus.com\/wp-content\/uploads\/2017\/05\/histogram.png 1000w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>This is a pretty boring looking histogram, but it&#8217;s extremely easy to modify it and add a more realistic set of data. <\/p>\n<h2>Wrapping Up<\/h2>\n<p>While this was just a quick run-through of some of GooPyCharts capabilities, I think we got a pretty good idea of what this charting package is capable of. It&#8217;s really easy to use, but only has a small set of charts to work with. <a href=\"http:\/\/pygal.org\/\" target=\"_blank\">PyGal<\/a>, Bokeh and <a href=\"http:\/\/matplotlib.org\/\" target=\"_blank\">matplotlib<\/a> have many other types of charts that they can create. However if you are looking for something that&#8217;s super easy to install and use and you don&#8217;t mind the small set of charts it supports, then GooPyCharts may be just the right package for you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last summer, I came across an interesting plotting library called GooPyCharts which is a Python wrapper for the Google Charts API. In this article, we will spend a few minutes learning how to use this interesting package. GooPyCharts follows syntax that is similar to MATLAB and is actually meant to be an alternative to matplotlib. [&hellip;]<\/p>\n","protected":false},"author":1891,"featured_media":6449,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[187,96,231],"class_list":["post-6436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visualizing-data","tag-data-visualisation","tag-plot","tag-python"],"views":8868,"_links":{"self":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/6436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/users\/1891"}],"replies":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/comments?post=6436"}],"version-history":[{"count":0,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/6436\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media\/6449"}],"wp:attachment":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media?parent=6436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/categories?post=6436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/tags?post=6436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}