Open In App

Introduction to Matplotlib

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

This article provides a brief Introduction to Matplotlib, introducing its core types and setting the foundation for further exploration into the library’s capabilities.

Introduction to Plotting with Matplotlib in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002. One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram, etc.

Installation: Windows, Linux, and macOS distributions have Matplotlib and most of its dependencies as wheel packages. Run the following command to install the Matplotlib package. But before that make sure Python and PIP are already installed on a system:

To install pip you can refer to this article : Python PIP

After checking Python and PIP in your system, You need to run this command to install Matplotlib.

python -m pip install -U matplotlib

 Importing Matplotlib

After successfully installing Matplotlib, You can use this command to import Matplotlib on your system.  

import matplotlib

Types of Matplotlib

Matplotlib comes with a wide variety of plots. Plots help to understand trends, and patterns, and to make correlations. They’re typically instruments for reasoning about quantitative information. Some of the sample plots are covered here.

  • Matplotlib Line Plot
  • Matplotlib Bar Plot
  • Matplotlib Histograms Plot
  • Matplotlib Scatter Plot
  • Matplotlib Pie Charts
  • Matplotlib Area Plot

Matplotlib Line Plot

By importing the matplotlib module, defines x and y values for a plotPython, plots the data using the plot() function and it helps to display the plot by using the show() function . The plot() creates a line plot by connecting the points defined by x and y values.

Python3




# importing matplotlib module
from matplotlib import pyplot as plt
 
# x-axis values
x = [5, 2, 9, 4, 7]
 
# Y-axis values
y = [10, 5, 8, 4, 2]
 
# Function to plot
plt.plot(x, y)
 
# function to show the plot
plt.show()


Output:

first-

Line Plot

Matplotlib Bar Plot

By using matplotlib library in Pythontwo, it allows us to access the functions and classes provided by the library for plotting. There are tow lists x and y are defined . This function creates a bar plot by taking x-axis and y-axis values as arguments and generates the bar plot based on those values.

Python3




# importing matplotlib module
from matplotlib import pyplot as plt
 
# x-axis values
x = [5, 2, 9, 4, 7]
 
# Y-axis values
y = [10, 5, 8, 4, 2]
 
# Function to plot the bar
plt.bar(x, y)
 
# function to show the plot
plt.show()


Output:

second

Bar Plot

Matplotlib Histograms Plot

By using the matplotlib module defines the y-axis values for a histogram plot. Plots in the ,histogram using the hist() function and displays the plot using the show() function. The hist() function creates a histogram plot based on the values in the y-axis list.

Python3




# importing matplotlib module
from matplotlib import pyplot as plt
 
# Y-axis values
y = [10, 5, 8, 4, 2]
 
# Function to plot histogram
plt.hist(y)
 
# Function to show the plot
plt.show()


Output:

third

Histogram

Matplotlib Scatter Plot

By imports,plot the matplotlib module, defines x and y values for a scatter plot, plots the data using the scatter() function, and displays the plot using the show() function. The scatter() function creates a scatter plot by plotting individual data points defined by the x and y values.

Python3




# importing matplotlib module
from matplotlib import pyplot as plt
 
# x-axis values
x = [5, 2, 9, 4, 7]
 
# Y-axis values
y = [10, 5, 8, 4, 2]
 
# Function to plot scatter
plt.scatter(x, y)
 
# function to show the plot
plt.show()


Output :  

fourth

Scatter Plot

We can also plot a scatter plot using the plot() method by providing the marker style as ‘o’.

Python3




# importing matplotlib module
from matplotlib import pyplot as plt
 
# x-axis values
x = [5, 2, 9, 4, 7]
 
# Y-axis values
y = [10, 5, 8, 4, 2]
 
# Function to plot scatter using plot() method
plt.plot(x, y, 'o')
 
# function to show the plot
plt.show()


Output:

five

Scatter Plot by using plot()

Matplotlib Pie Charts

By importing the module Matplotlib in Python to create a pie chart with three categories and respective sizes. The plot .pie() function is used to generate the chart, including labels, percentage formatting, and a starting angle. A title is added with plt. title(), and plt. show() displays the resulting pie chart, visualizing the proportional distribution of the specified categories.

Python3




import matplotlib.pyplot as plt
 
# Data for the pie chart
labels = ['Geeks 1', 'Geeks 2', 'Geeks 3']
sizes = [35, 35, 30]
 
# Plotting the pie chart
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart Example')
plt.show()


Output:

pie

Pie Chart

Matplotlib Area Plot

By importing Matplotlib we sky bluegenerated an area chart with two lines (‘Line 1’ and ‘Line 2’). The area between the lines is shaded in a skyblue color with 40% transparency. The x-axis values are in the list ‘x’, and the corresponding y-axis values for each line are in ‘y1’ and ‘y2’. Labels, titleslegends, and legend are added, and the resulting area chart is displayed.

Python3




import matplotlib.pyplot as plt
 
# Data
x = [1, 2, 3, 4, 5]
y1, y2 = [10, 20, 15, 25, 30], [5, 15, 10, 20, 25]
 
# Area Chart
plt.fill_between(x, y1, y2, color='skyblue', alpha=0.4, label='Area 1-2')
plt.plot(x, y1, label='Line 1', marker='o')
plt.plot(x, y2, label='Line 2', marker='o')
 
# Labels and Title
plt.xlabel('X-axis'), plt.ylabel('Y-axis'), plt.title('Area Chart Example')
 
# Legend and Display
plt.legend(), plt.show()


Output :

last-

Area Plot

Conclusion

In conclusion , this article has helped us understand the basics of the library and its main types. Matplotlib is a powerful tool for creating visualizations in Python, providing many options for different types of plots. As users become more familiar with Matplotlib, they can use its various plot styles and customization options to make appealing and informative graphics for understanding and presenting data. This overview marks the starting point for exploring and utilizing Matplotlib’s capabilities in various data science and visualization projects.



Last Updated : 29 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads