Creating Graphs With Python and GooPyCharts
If you are looking for something that’s super easy to install and use, and you don’t mind the small set of charts it supports, then GooPyCharts may be just the right package for you.
Join the DZone community and get the full member experience.Join For Free
over the 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.
to install goopycharts, all you need to do is use pip like this:
pip install gpcharts
now that we have it installed, we can give it a whirl!
our first graph
using goopycharts to create a chart or graph is extremely easy. in fact, you can create a simple graph in 3 lines of code:
>>> from gpcharts import figure >>> my_plot = figure(title='demo') >>> my_plot.plot([1, 2, 10, 15, 12, 23])
if you run this code, you should see your default browser pop open with the following image displayed:
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.
creating a bar graph
the goopycharts package has a nice testgraph.py script included to help you learn how to use the package. unfortunately, it doesn’t actually demonstrate different types of charts. so i took one of the examples from there and modified it to create a bar chart:
from gpcharts import figure fig3 = figure() xvals = ['temps','2016-03-20','2016-03-21','2016-03-25','2016-04-01'] yvals = [['shakuras','korhal','aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]] fig3.title = 'weather over days' fig3.ylabel = 'dates' fig3.bar(xvals, yvals)
you will note that in this example we create our title using the figure instance’s title property. we also set the ylabel 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 plot we need to call bar to generate a bar chart. here is the result:
creating other types of graphs
let’s modify the code a bit more and see if we can create other types of graphs. we will start with a scatter plot:
from gpcharts import figure my_fig = figure() xvals = ['dates','2016-03-20','2016-03-21','2016-03-25','2016-04-01'] yvals = [['shakuras','korhal','aiur'],[10,30,40],[12,28,41],[15,34,38],[8,33,47]] my_fig.title = 'scatter plot' my_fig.ylabel = 'temps' my_fig.scatter(xvals, yvals)
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. when you run this code, you should see something like this:
that was pretty simple. let’s try creating a quick and dirty histogram:
from gpcharts import figure my_fig = figure() my_fig.title = 'random histrogram' my_fig.xlabel = 'random values' vals = [10, 40, 30, 50, 80, 100, 65] my_fig.hist(vals)
the histogram is much simpler than the last two charts we created as it only needs one list of values to create it successfully. this is what i got when i ran the code:
this is a pretty boring looking histogram, but it’s extremely easy to modify it and add a more realistic set of data.
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’s really easy to use, but only has a small set of charts to work with. pygal , bokeh, and matplotlib have many other types of charts that they can create. however, if you are looking for something that’s super easy to install and use and you don’t mind the small set of charts it supports, then goopycharts may be just the right package for you!
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.