Sunday 9 August 2015

Python-Charts


                Creating Charts


In this blog am going to discuss about creating graphical charts with the data using matplotlib library.
Click on the matplotlib hyperlink to know more about Visualizations in python using matplotlib.

About matplotlib

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell, web application servers, and six graphical user interface toolkits.
matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For a sampling, see the screenshotsthumbnail gallery, and examples directory.
For simple plotting the pyplot interface provides a MATLAB-like interface, particularly when combined with IPython. For the power user, you have full control of line styles, font properties, axes properties, etc, via an object oriented interface or via a set of functions familiar to MATLAB users.

Inline Charts

For windows users ,  when you are running IPython Notebook or IPython QtConsole you need to run the following command that tells ipython that you want charts to be shown "inline style" inside your notebook, not in a separate window.

To start with, here is an example of a simple chart using Ipython Notebook*

*If you are using Python instead of Ipython Notebook you just need to add one final line to each of your 
programs. This will display a window with the chart you created, and pause the script until you close it.


Let us take the example from the previous blog (Survey – Radish Variety Program).

We are going to use matplotlib to generate bar graph to display the vote counts. First load the data into the console. 





Now we are going to import pyplot and NumPy 

Pyplot - pyplot is one way to plot graph data with Matplotlib.
NumPy - NumPy is a module providing lots of numeric functions for Python.





Now we are going to split the dictionary names – Votes into to lists, one for the names and other for the vote counts. Below loop processes the dictionary into a format that's easy to send to matplotlib - a list of radish names (for the labels on the bars) and a list of vote counts (for the actual graph.).
we are then creating a range of indexes for the X values in the graph, one entry for each entry in the "counts" dictionary. This will spread out the graph bars evenly across the X axis on the plot. np.arange is a NumPy function like the range() function in Python, only the result it produces is a "NumPy array". We'll see why this is useful in a second.



Using plt.bar() we are creating a bar graph using the "x" values in X axis and the vote counts in Y 
asis.


plt.xticks() specifies a range of values to use as labels ("ticks") for the X axis.



Advanced Charting

matplotlib and pyplot are both extremely powerful charting frameworks.
To take a look at some of what they can do (and the sample Python code that does it), take a moment to browse through the Matplotlib thumbnail gallery and the Pyplot tutorial.
Because these tools are fairly complex it can also be helpful to copy and tweak an existing example from the gallery, if you're looking to create a new chart.
(You'll notice the term "pylab" used on some of those pages. Pylab just means Pyplot combined with Numpy.)




No comments:

Post a Comment