Sunday 9 August 2015

Knowing Python Part 2

Visualisation in Python

Lets continue from where we left lets create a few charts and graphs based on the results we got from our Voting Count section.

An important library called matplotlib is used in python to create graphical charts.We will take the example from the previous blog and generate bar graph to display the vote counts.Lets first load the data in the console.
Remember : We need to add an extra line to our code if using python instead of ipython.
i.e plot.show().

Lets start creating a bar graph



Lets go line by line and understand the above code.
1.) First we import the modules numpy and pyplot.
2.) Then we have split the dictionary into two (i)Names (ii) Votes.
3.) We create a range of indexes for the x values in the graph,one entry for each item in the counts             dictionary numbered 1,2,3 and so on, this will help in spreading the bar graphs evenly across the         x-axis.
4.) plt.bar() - it creates a bar graph,using x values as the x-axis and the values in the votes array as the      height of each bar.
5.) plt.xticks(x+0.5,names,rotation=90) - This gives labels to the x-axis.


Source: http://opentechschool.github.io/python-data-intro/core/charts.html

No comments:

Post a Comment