December 12, 2023
Python

# Data Visualization using Matplotlib

Matplotlib is the most popular Python library for Data Visualization. It is a multi-platform, 2D plotting library and supports a wide variety of Operating Systems. In this article, we will focus on Data Visualization using matplotlib.

## matplotlib

We generally import matplotlib as:

Let’s consider the following sales_records data for visualization using matplotlib:

The DataFrame is:

This sales_records dataset consists of the sales profile of three companies over the years. Let’s plot some graphs to visualize this data more clearly.

## Line Plot

A line plot is useful to visualize the frequency of data along the number line. This is highly useful in the case of Time-series data. We can visualize the trend in Sales of the Company1 over the given 10 years using a line graph. The code for doing so using Matplotlib is:

Output:

plt.xlabel() is used to label the x-axis. Similarly, plt.ylabel() labels the y-axis. plt.show() is used to display the plot. The color of the plot can also be modified. For example, to get the line plot in red color:

Output:

We can also view the Sales trend of three companies together in the same plot, as:

Output:

plt.title() is used to specify the title of the chart. plt.legend() displays associated legend.

Functions can be plotted using line graphs of matplotlib, as:

Output:

## Bar Graph

Data can also be visualized using horizontal or vertical straight lines. To do so in matplotlib:

Output:

Multiple variables can also be represented on the same graph. This can be done with some modifications as:

Output:

## Histogram

A histogram is useful for showing distribution frequency for continuous data. For example, for the generated random data, we can view the distribution, as:

Output:

## Pie Chart

We can also visualize the sales data using a Pie chart. This is useful for a quick comparison between the quantities. Consider the example for the following data:

Output:

## Stack Plot

We can also plot a stack plot where data of different categories are stacked together. This is an extension of the line chart and bar plot. For example, for sales_records data, the stack plot is:

Output:

## Scatter Plot

To view data in form of Scatter plots, can be done using matplotlib as:

Output:

## Box Plots

Box plot displays from 1st to 3rd quartile of a set of data containing the minimum, maximum, first quartile, third quartile, and median. Consider the example on the following random data:

Output:

## Summary

In this article, we worked with Data Visualization using Matplotlib, its various features, and the different types of graphical representations that can be achieved through it. In the next tutorial, we will focus on visualization using Seaborn.