top of page

Python Data Visualization with Matplotlib

Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits. The library provides functions for plotting line plots, bar plots, error bars, scatter plots, histograms, bar charts, pie charts, box plots, violin plots, and more.

Matplotlib is a 2D plotting library in Python that enables users to create a wide range of static, animated, and interactive visualizations. It provides a high-level interface for drawing attractive and informative statistical graphics.

Some of the key features of Matplotlib include:

  1. Plotting various types of plots, including line plots, scatter plots, bar plots, histograms, pie charts, box plots, violin plots, and more.

  2. Support for plotting data with multiple X and Y axes on the same figure.

  3. Customization options, including color maps, markers, line styles, and text annotations.

  4. Saving figures to various file formats, including PNG, PDF, SVG, and JPG.

  5. The Interactive mode that allows users to zoom, pan, and toggle plot elements.

Create a line Plot

STEP 1: Import the Matplotlib library:python

import matplotlib.pyplot as plt 

STEP 2: Prepare the data to be plotted:python

x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] 

STEP 3: Create a figure and axis:python

fig, ax = plt.subplots() 

STEP 4: Plot the data:python

ax.plot(x, y) 

STEP 5: Add labels and title to the plot:python

ax.set(xlabel='X-axis label', ylabel='Y-axis label',       
title='Title of the Plot') 

STEP 6: Show the plot:python

Types of plots in Data Visualization

In the Matplotlib library, there are several types of plots for data visualization:

Line plot: A line plot is a way to display data along a number axis. It is used to show the trend of data over time or to compare the data between categories.

Scatter plot: A scatter plot displays individual data points on a 2D plane. It is used to observe the relationship between two variables.

Bar plot: A bar plot represents categorical data with rectangular bars. The height of the bar represents the value of the data. It is used to compare the magnitude of data across categories.

Histogram: A histogram represents the distribution of a set of continuous or numerical data by dividing the entire range of values into a series of intervals and counting the number of values that fall into each interval.

Pie chart: A pie chart represents data as slices of a circle, where the size of each slice represents the proportion of the data it represents. It is used to show the composition of data across categories.

Box plot: A box plot represents the distribution of a set of numerical data by displaying a box that spans from the lower quartile to the upper quartile of the data, with a line inside the box representing the median value. It is used to show the spread and skewness of the data.

Violin plot: A violin plot is similar to a box plot, but also shows the density of the data. It is used to show the distribution of data across categories.

Swarm plot: A swarm plot is a scatter plot where the points are adjusted along the y-axis so that they don’t overlap. It is used to show the distribution of data across categories.

Area plot: An area plot is a line plot where the area below the line is filled with color or shading. It is used to show the proportion of the data across categories.

Hexbin plot: A hexbin plot is a way to visualize the relationship between two variables by dividing the data into hexagonal bins and coloring each bin based on the count of data points within it. It is used to visualize dense data.

Contour plot: A contour plot is a way to visualize 3D data on a 2D plane by representing values with contour lines. It is used to show the relationship between three variables.

Quiver plot: A quiver plot is a way to visualize vector fields by plotting arrows that represent the direction and magnitude of the vectors. It is used to show the flow of a physical quantity.

Why do you need Matplotlib in Data Visualization?

Matplotlib is a valuable tool in data visualization because it offers a wide range of features and customization options for creating visual representations of data. There are several reasons why you might want to use Matplotlib for data visualization, including:

  1. Flexibility: Matplotlib is a highly flexible library that can handle a wide range of data and visualization needs. Whether you are looking to create simple line plots or complex visualizations with multiple subplots, Matplotlib has the tools you need.

  2. Customization: Matplotlib provides a comprehensive set of customization options for visualizing data, including color maps, markers, line styles, and text annotations. This allows you to fine-tune the appearance of your visualizations to meet your needs.

  3. Interactivity: Matplotlib supports interactive mode, which allows you to zoom, pan, and toggle plot elements in real-time. This can be a valuable tool for exploring and analyzing data.

  4. Output Formats: Matplotlib supports a wide range of output formats, including PNG, PDF, SVG, and JPG, making it easy to save and share your visualizations.

  5. Integration with other tools: Matplotlib integrates well with other data analysis and visualization tools, such as NumPy, Pandas, and Seaborn. This allows you to build complex data analysis pipelines and create rich visualizations of your data.


Matplotlib is a powerful and versatile data visualization tool that offers a high degree of flexibility, customization, and interactivity. Whether you are a data scientist, data analyst, or data visualization specialist, Matplotlib is an essential tool to have in your data visualization toolkit.


bottom of page