Getting started with matplotlib (Pyplot)

ASHWIN.S
Artificialis
Published in
3 min readJul 22, 2022
Title banner

Data Visualization is one of the key skills required for data scientists. There are many tools avaliable for visualizing the data graphically. One such tool for python developers is matplotlib.

Matplotlib is open source cross-plotform python library used for Data visualization and Graphical plotting. In this tutorial we will look in to the basics of matplotlib library.

Installing Matplotlib

For installing matplotlib in windows, press win+R and type cmd and click enter. in the command prompt type the following command.

pip install matplotlib

For Linux users, press ctrl+alt+T to open the Terminal. And type the following command.

sudo apt-get install python3-matplotlib 

Instead of setting up the environment it is recommended to use Jupyter notebook for matplotlib. Anaconda is a package manager that has various libraries in it including Jupyter notebook.

Download anaconda from here.

Getting started

import the matplotlib library using the following code.

import matplotlib.pyplot as mlt

After importing, type the folloing code for plotting a basic graph

# plotting the data
plt.plot([10, 20, 30, 40], [20, 30, 40, 50])
plt.show()
Basic line plot

X-axis and Y-axis are given as arguments in plt.show(). They are in list datatype.

Lets give a title for our plot and lables for X-axis and Y-axis.

import matplotlib.pyplot as plt# plotting 
plt.plot([10, 20, 30, 40], [20, 30, 40, 50])
# A title for our plot
plt.title("Sample Plot")
# labels
plt.ylabel("y-axis")
plt.xlabel("x-axis")
plt.show()

Bar Graph

Bar graph is plotted using plt.bar()

import matplotlib.pyplot as plt# datasetcourses = ['Python','Java','C++', 'C']
values = [24, 12, 6, 18]
# bar plot
plt.bar(courses, values, color = 'orange')
plt.xlabel("Courses")
plt.ylabel("Number of students")
plt.title("Enrollment")
plt.show()

Scatter plot

scatter plot is done in matplotlib using plt.scatter()

import matplotlib.pyplot as plt

# dataset
x = [2,3,4,6,7,9,8,9,8,12]
y = [34,65,34,60,83,45,67,87,78,90]

plt.scatter(x, y, c ="blue")
plt.show()

Pie chart

Pie chart is often used to illustrate the numerical proportion of a categorical variable. It is plotted using plt.pie()

import matplotlib.pyplot as plt# dataset
count = [35, 25, 25, 15]
lang = ["Python", "Java", "C++", "C"]
plt.pie(count, labels = lang)
plt.legend()
plt.show()

here the plt.legend() is used to mark the legends.

Above mentioned are only few examples. Many types of plots are avalible in matplotlib. Almost all the plots are customizable, that is you can change the apperance of every elements in any given graph.

Documentation

The official documentation of matplotlib is given here.

conclusion

This must be a good start for your matplotlib journey. Go through the Documentation to learn more. Do share and follow for more.

Happy Learning :)

--

--

ASHWIN.S
Artificialis

I write about Machine Learning and Data Science. | Python Developer | ML enthusiast |Human Being.