DEV Community

loading...

Tutorial Matplotlib

George Karanikolas
Mobile Developer, IT Student
Updated on ・2 min read

What is Matplotlib?

Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. It provides an object-oriented API that helps in embedding plots in applications using Python GUI toolkits such as PyQt, WxPythonotTkinter. It can be used in Python and IPython shells, Jupyter notebook and web application servers also.
Matplotlib is often used along with package like NumPy.

Install Matplotlib

pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

Import Matplotlib

import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Create Title

plt.title("Name")
Enter fullscreen mode Exit fullscreen mode

Create X and Y Labels

plt.xlabel('Name')
plt.ylabel('Name')
Enter fullscreen mode Exit fullscreen mode
plt.xlabel('Name', color='red')
plt.ylabel('Name', color='#ff0000')
Enter fullscreen mode Exit fullscreen mode

Create Plot

plt.plot()
Enter fullscreen mode Exit fullscreen mode
plt.plot(name_x, name_y)
Enter fullscreen mode Exit fullscreen mode
plt.plot(name_x, name_y, color='#ff0000')
Enter fullscreen mode Exit fullscreen mode
plt.plot(name_x, name_y, color='#ff0000', linestyle='--')
Enter fullscreen mode Exit fullscreen mode
plt.plot(name_x, name_y, color='#ff0000', linestyle='--', label="Name")
Enter fullscreen mode Exit fullscreen mode

Add Labels in Plot

plt.legend()
Enter fullscreen mode Exit fullscreen mode

Show Plot

plt.show()
Enter fullscreen mode Exit fullscreen mode

Save Plot In Image

plt.savefig('name.png')
Enter fullscreen mode Exit fullscreen mode
plt.savefig('name.png', transparent = True) # Transparent Figure
Enter fullscreen mode Exit fullscreen mode

Close And Clear

plt.cla() # Clear an axis
plt.clf() # Clear the entire figure
plt.close() # Close a window
Enter fullscreen mode Exit fullscreen mode

Examples:

Example 1:
import matplotlib.pyplot as plt

years_x = [2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
greece_y = [8.4,7.8,9.6,12.7,17.9,24.5,27.5,26.5,24.9,23.6,21.5,19.3]
europe_y = [7.2,7,9,9.6,9.7,10.5,10.9,10.2,9.4,8.6,7.6,6.8]

plt.title("Comparison of Unemployment")

plt.plot(years_x, greece_y, color='#ff0000', label="Greece")
plt.plot(years_x, europe_y, color='#00ff00', label="Europe", linestyle='--')

plt.xlabel('Years', color='#ff0000')
plt.ylabel('Percentage %', color='#ff0000')

plt.savefig('plot.png')

plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Github: https://github.com/SeijinD/Python-World/blob/master/main/extends_libraries/matplotlib.md

Discussion (0)