Types of Graphs in Matplotlib

Saaisri
featurepreneur
Published in
3 min readJul 31, 2022

In this article we will review about the major types of graphs in matplotlib and what graph is to be used for specific functionality.

Barplot

Barplot is used for categorical representation. The values in a bargraph are dependent on each other. It 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. One of the axis of the plot represents the specific categories being compared, while the other axis represents the measured values corresponding to those categories.

Syntax to write Barplot:

plt.bar(x, height, width, bottom, align)

An example of Barplot:

Scatter Chart

Scatter Chart is used to represent a cluster to see the range of the variables. They are used to plot data points on horizontal and vertical axis in the attempt to show how much one variable is affected by another. Each row in the data table is represented by a marker the position depends on its values in the columns set on the X and Y axes. A third variable can be set to correspond to the color or size of the markers, thus adding yet another dimension to the plot.

You can use the scatter() function to draw a scatter plot.

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)
plt.show()

Result:

Line Chart

Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis.

import matplotlib.pyplot as pltimport numpy as npx = np.array([1, 2, 3, 4]) y = x*2 plt.plot(x, y) plt.show()

Output

Histogram

Histogram is used as a count plot. It is used to detect how many values lie in a given range.It is an accurate representation of the distribution of numerical data. It is an estimate of the probability distribution of a continuous variable. It is a kind of bar graph. The parameters required to construct a histogram are x, bins, range, density, cimulative, histtype.

An example of histogram:

N_points = 100000
n_bins = 20

dist1 = rng.standard_normal(N_points)
dist2 = 0.4 * rng.standard_normal(N_points) + 5

fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True)

axs[0].hist(dist1, bins=n_bins)
axs[1].hist(dist2, bins=n_bins)

Box Plot

A Box Plot is also known as Whisker plot is created to display the summary of the set of data values having properties like minimum, first quartile, median, third quartile and maximum. In the box plot, a box is created from the first quartile to the third quartile, a vertical line is also there which goes through the box at the median. Here x-axis denotes the data to be plotted while the y-axis shows the frequency distribution.

A box plot is a type of plot that displays the five number summary of a dataset, which includes:

  • The minimum value
  • The first quartile (the 25th percentile)
  • The median value
  • The third quartile (the 75th percentile)
  • The maximum value

The interquartile range, often abbreviated IQR, is the difference between the third quartile and the first quartile.

  • IQR = Q3 — Q1

This tells us how spread out the middle 50% of values are in a given dataset.

--

--