How to generate statistical graphs using Python?

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

#Showing what we plotted

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:


There are many types of charts like bar graph, scatterplots, pie chart, etc. available. You can learn more about these on

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started