Important Matplotlib Functions for Data Science

Matplotlib is one of the most preferred Python libraries for data visualization. If you are learning data science, you must be aware of the kinds of data visualizations you need to work with. In this article, I will not focus on how to create data visualizations with matplotlib. Here, I will take you through all the important matplotlib functions for data science that you should know for creating beautiful and self-explanatory data visualizations using Python.

Most Important Matplotlib Functions for Data Science

Matplotlib is an amazing Python library for data visualization. Almost all types of data visualization charts can be created using matplotlib. If you want to learn all the data visualizations you need to know for data science and how to create them, you can check out this article. Here I will walk you through the most important functions of matplotlib that you need when creating any kind of data visualization.

Now let’s import a dataset and the necessary Python libraries that we need to create a data visualization:

import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("Instagram.csv", encoding="latin-1")
print(data.head())
   Impressions  From Home  From Hashtags  From Explore  From Other  Saves  \
0       3920.0     2586.0         1028.0         619.0        56.0   98.0   
1       5394.0     2727.0         1838.0        1174.0        78.0  194.0   
2       4021.0     2085.0         1188.0           0.0       533.0   41.0   
3       4528.0     2700.0          621.0         932.0        73.0  172.0   
4       2518.0     1704.0          255.0         279.0        37.0   96.0   

   Comments  Shares  Likes  Profile Visits  Follows  \
0       9.0     5.0  162.0            35.0      2.0   
1       7.0    14.0  224.0            48.0     10.0   
2      11.0     1.0  131.0            62.0     12.0   
3      10.0     7.0  213.0            23.0      8.0   
4       5.0     4.0  123.0             8.0      0.0   

                                             Caption  \
0  Here are some of the most important data visua...   
1  Here are some of the best data science project...   
2  Learn how to train a machine learning model an...   
3  Here’s how you can write a Python program to d...   
4  Plotting annotations while visualizing your da...   

                                            Hashtags  
0  #finance #money #business #investing #investme...  
1  #healthcare #health #covid #data #datascience ...  
2  #data #datascience #dataanalysis #dataanalytic...  
3  #python #pythonprogramming #pythonprojects #py...  
4  #datavisualization #datascience #data #dataana...  

The dataset that I am using here is about the reach of my Instagram account. You can find this dataset here. To implement all the important matplotlib functions, I will first create a line plot, and then I will show you the implementation of all the important matplotlib functions one by one. So let’s create a line plot of my Instagram reach from various sources as mentioned in the dataset:

# Creating a Line Plot
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.show() # for visualizing your graph
line plot for important matplotlib functions

Customizing Figure Size:

The plt.figure(figsize=(float, float)) helps us to customize the size of our graphs. Here’s how to use this function:

# Customizing Figure Size
plt.figure(figsize=(15, 10)) # Customizing Figure Size
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.show()
Customizing Figure Size in graph

Customizing Theme:

The plt.style.use(“style name”) helps us customize the theme of our graphs. For now, I will be using the “fivethirtyeight” theme style of matplotlib. Here’s how to use this function:

# Customizing Themes
plt.style.use('fivethirtyeight') # for customizing theme
plt.figure(figsize=(15, 10))
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.show()
Customizing Themes: important matplotlib functions

Adding Title:

Adding a title to your graphs is always a good habit. It helps in describing the purpose of the data visualization. Here is how to add the title to our graphs:

# Adding a Title to Your Graph
plt.style.use('fivethirtyeight')
plt.figure(figsize=(15, 10))
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.title("Impressions on Instagram from Various Sources") # for adding a title
plt.show()
for adding a title in a graph

Adding Labels on the Axes

The xlabel() and ylabel() functions will help us add labels to the x and y axes of our charts. Here’s how to use these functions:

# Adding Labels on xaxis and yaxis
plt.style.use('fivethirtyeight')
plt.figure(figsize=(15, 10))
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.title("Impressions on Instagram from Various Sources")
plt.xlabel("Days") # adding label on xaxis
plt.ylabel("Impressions") # adding label on yaxis
plt.show()
adding labels on axes in a graph

Adding Legend

A graph’s legend displays the labels and colours for each feature displayed in the graph. Here’s how to add a legend to our graphs:

# Adding Legend
plt.style.use('fivethirtyeight')
plt.figure(figsize=(15, 10))
plt.plot(data["From Home"], "-r", label="From Home")
plt.plot(data["From Hashtags"], "-g", label="From Hashtags")
plt.plot(data["From Explore"], "-b", label="From Explore")
plt.plot(data["From Other"], "-k", label="Other")
plt.title("Impressions on Instagram from Various Sources")
plt.xlabel("Days")
plt.ylabel("Impressions")
plt.legend(title="Instagram Reach") # for adding legend with a title
plt.show()
for adding legend with a title: important matplotlib functions

So these are the most important matplotlib functions you should know about. All the functions mentioned in this article are easy to use. Without these functions, you can create a data visualization, but your data visualizations may not be self-explanatory to people who didn’t create them. So, as a data science professional, you need to create graphs in a way that anyone can understand. That’s why you should implement all these functions to make your charts more beautiful and explanatory.

Summary

So these were the most important functions in matplotlib that will help you create beautiful and more explanatory graphs while creating any kind of data visualization. Matplotlib is an amazing Python library for Data Visualization. Almost all types of data visualization graphs can be made using matplotlib. I hope you liked this article on the important matplotlib functions for data science. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1211

Leave a Reply