Data Visualization using Matplotlib in Python
Data Visualization using Matplotlib in Python
We can also install matplotlib in Jupyter Notebook and Google colab environment
by using the same command:
If you are using Jupyter Notebook, you can install it within a notebook cell by
using:
!pip install matplotlib
Basic Plotting with Matplotlib : Step-by-Step Examples
Matplotlib supports a variety of plots including line charts, bar charts, histograms,
scatter plots, etc. We will discuss the most commonly used charts in this article
with the help of some good examples and will also see how to customize each
plot.
1. Line Chart
Line chart is one of the basic plots and can be created using the plot() function. It
is used to represent a relationship between two data X and Y on a different axis.
Syntax:
matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
Example:
Python
1
import matplotlib.pyplot as plt
plt.show()
Output:
2. Bar Chart
A bar chart is a graph that represents the category of data with rectangular
bars with lengths and heights that is proportional to the values which they
represent. The bar plots can be plotted horizontally or vertically. A bar chart
describes the comparisons between the discrete categories. It can be created
using the bar() method.
In the below example, we will use the tips dataset. Tips database is the record of
the tip given by the customers in a restaurant for two and a half months in the early
1990s. It contains 6 columns as total_bill, tip, sex, smoker, day, time, size.
Example:
Python
2
data = pd.read_csv('tips.csv')
plt.show()
Output:
3. Histogram
A histogram is basically used to represent data provided in a form of some
groups. It is a type of bar plot where the X-axis represents the bin ranges while the
Y-axis gives information about frequency. The hist() function is used to compute
and create histogram of x.
Syntax:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None,
cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’,
rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \
*\*kwargs)
Example:
Python
3
# initializing the data
x = data['total_bill']
plt.show()
Output:
4. Scatter Plot
Scatter plots are used to observe relationships between variables.
The scatter() method in the matplotlib library is used to draw a scatter plot.
Syntax:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None,
marker=None, cmap=None, vmin=None, vmax=None, alpha=None,
linewidths=None, edgecolors=None
Example:
Python
4
# Adding title to the plot
plt.title("Tips Dataset")
plt.show()
Output:
5. Pie Chart
Pie chart is a circular chart used to display only one series of data. The area of
slices of the pie represents the percentage of the parts of the data. The slices of
pie are called wedges. It can be created using the pie() method.
Syntax:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None,
autopct=None, shadow=False)
Example:
Python
plt.show()
Output:
5
6. Box Plot
A Box Plot, also known as a Whisker Plot, is a standardized way of displaying the
distribution of data based on a five-number summary: minimum, first quartile (Q1),
median (Q2), third quartile (Q3), and maximum. It can also show outliers.
Let’s see an example of how to create a Box Plot using Matplotlib in Python:
Python
np.random.seed(10)
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.xlabel('Data Set')
plt.ylabel('Values')
plt.title('Example of Box Plot')
plt.show()
Output:
6
Explanation:
plt.boxplot(data): Creates the box plot. The vert=True argument makes the
plot vertical, and patch_artist=True fills the box with color.
boxprops and medianprops: Customize the appearance of the boxes and median
lines, respectively.
The box shows the interquartile range (IQR), the line inside the box shows the
median, and the “whiskers” extend to the minimum and maximum values within 1.5
* IQR from the first and third quartiles. Any points outside this range are
considered outliers and are plotted as individual points.
7. Heatmap
A Heatmap is a data visualization technique that represents data in a matrix form,
where individual values are represented as colors. Heatmaps are particularly
useful for visualizing the magnitude of data across a two-dimensional surface and
identifying patterns, correlations, and concentrations.
Let’s see an example of how to create a Heatmap using Matplotlib in Python:
Python
np.random.seed(0)
data = np.random.rand(10, 10)
# Create a heatmap
plt.imshow(data, cmap='viridis', interpolation='nearest')
# Add a color bar to show the scale
plt.colorbar()
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Example of Heatmap')
plt.show()
Output:
Explanation:
7
plt.imshow(data, cmap='viridis') : Displays the data as an image (heatmap).
The cmap='viridis' argument specifies the color map used for the heatmap.
interpolation='nearest' : Ensures that each data point is shown as a block of
color without smoothing.
The color bar on the side provides a scale to interpret the colors, with darker colors
representing lower values and lighter colors representing higher values. This type
of plot is often used in fields like data analysis, bioinformatics, and finance to
visualize data correlations and distributions across a matrix.
Advanced Plotting: Techniques for Visualizing Subplots
We have learned about the basic components of a graph that can be added so that
it can convey more information. One method can be by calling the plot function
again and again with a different set of values as shown in the above example. Now
let’s see how to plot multiple graphs using some functions and also how to plot
subplots.
Method 2: Using subplot() method
This method adds another plot at the specified grid position in the current figure.
Syntax:
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
Example:
Python
8
Error bars always run parallel to a quantity of scale axis so they can be displayed
either vertically or horizontally depending on whether the quantitative scale is on
the y-axis or x-axis if there are two quantity of scales and two pairs of arrow bars
can be used for both axes.
# importing matplotlib
x =[1, 2, 3, 4, 5, 6, 7]
9
y =[1, 2, 1, 2, 1, 2, 1]
# plotting graph
plt.plot(x, y)
Output:
# importing matplotlib
import matplotlib.pyplot as plt
# creating error
y_error = 0.2
# plotting graph
plt.plot(x, y)
plt.errorbar(x, y,
yerr = y_error,
fmt ='o')
Output:
10
Three-dimensional Plotting in Python using
Matplotlib
Last Updated : 22 Dec, 2023
3D plots are very important tools for visualizing data that have three
dimensions such as data that have two dependent and one independent
variable. By plotting data in 3d plots we can get a deeper understanding
of data that have three variables. We can use various matplotlib library
functions to plot 3D plots.
Example Of Three-dimensional Plotting using Matplotlib
We will first start with plotting the 3D axis using the Matplotlib library. For
plotting the 3D axis we just have to change the projection parameter
of plt.axes() from None to 3D.
Python3
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
Output:
11
3D Plots in MATLAB
Last Updated : 09 May, 2021
Output:
12
3D Heatmap in Python
Last Updated : 28 Jul, 2021
# creating figures
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
# displaying plot
plt.show()
Output:
14
15