How to generate statistical graphs using Python?

PythonProgramming

Python has an amazing graph plotting library called matplotlib. It is the most popular graphing and data visualization module for Python. You can start plotting graphs using 3 lines! For example,

from matplotlib import pyplot as plt

# Plot to canvas
plt.plot([1,2,3],[4,5,1])

#Showing what we plotted
plt.show()

This will create a simple graph with coordinates (1,4), (2,5) and (3,1). You can Assign labels to the axes using the xlabel and ylabel functions. For example,

plt.ylabel('Y axis')
plt.xlabel('X axis')

And also provide a title using the title function −

plt.title('My Graph')

Remember to call these before the show() call as it will render these only if called before show. You can also style these graphs by providing arguments like linewidth=2 to the plt.plot function. You can also use the legend() function to create a legend:

plt.legend()

There are many types of charts like bar graph, scatterplots, pie chart, etc. available. You can learn more about these on https://pythonprogramming.net/matplotlib-python-3-basics-tutorial/

raja
Published on 20-Apr-2018 07:32:09
Advertisements