The world’s leading publication for data science, AI, and ML professionals.

Python Data Visualization with Matplotlib – Part 1

Completed Matplotlib tutorials for Python plotting from basic to advanced, with 100+ examples

Photo by Jukan Tateisi on Unsplash
Photo by Jukan Tateisi on Unsplash

Introduction to Matplotlib

Data Visualization aims to present the data into a more straightforward representation, such as scatter plot, density plot, bar chart, etc. It is also useful to give readers or analysts a global picture of their data. By visualizing your data, you can detect potential outliers. In Python, you can use various modules or libraries to visualize data. One of the mainstream modules is Matplotlib. You can visualize data using Matplotlib in various plotting styles. But, Matplotlib can not show you a dynamics plot. If you want to create a tremendous dynamic plot, you can use Dash from plotly (I hope to finish a story about a complete tutorial with Dash next month).

This story will guide you on how to visualize data with Matplotlib in a various way. 90 examples maybe can inspire you to create a plot from different points of view. It is not the most completed tutorials in data visualization with Matplotlib, but I believe that this story can accommodate many people’s needs, reach many disciples to be applied.

As I mentioned before, I will guide you through creating 90 various plot examples. The examples are distributed in 11 different style plots: scatter plot, line plot, histogram 1D, 2D, marginal plot, bar chart, box plot, violin plot, pie chart, polar chart, geographic projection, 3D plot, and contour plot. You can see Figure 1 to have a general idea of this story.

Figure 1. Various types of plot generated in Matplotlib (Image by Author).
Figure 1. Various types of plot generated in Matplotlib (Image by Author).

In this story, I try to focus on creating and customizing various plots. So, I assume you have known several techniques outside it, e.g., creating multiple subplots and customizing colormaps in Matplotlib. If you have not known it, I will give you some links to understand it.

At the beginning of writing this story, I plan to write only 1 story. But, I think I need to divide it into several parts because of the reading time. If I write it all in one story, it will cost more than 40 minutes. I guess you will be bored to read it all just in one story :). So, I divide it into 2 or 3 parts. I will limit the reading time to less than 30 minutes. This is the first part. Let’s start it.

Introduction to Matplotlib

To install Matplotlib, you can install it via pip with this code

pip install matplotlib

or via conda

conda install -c anaconda matplotlib

When I write this story, I installed Matplotlib version 3.3.2. You can check it by writing this code.

pip show matplotlib

If you want to check it in Jupyter Notebook (hereafter, Jupyter), you can add ! before pip, as shown in Figure 2.

Figure 2. Matplotlib version checking via Jupyter (Image by Author).
Figure 2. Matplotlib version checking via Jupyter (Image by Author).

If you want to upgrade your Matplotlib version, you can use the following code

pip install matplotlib --upgrade

Again, you can add ! before pip to upgrade Matplotlib via Jupyter.

If you have installed a different Matplotlib version with Matplotlib I used in this story, maybe you will face different results. Just write the issues you meet in the response columns below. I recommend you to save this story. So, you can continue your reading if you have limited time.

Before we move on the first section, I need to inform you that I have customized my Matplotlib plotting style such as using LaTeX font as default, changing the font size and family, changing xtick and ytick direction and size, and adding minor tick in the x-axis and y-axis. To use LaTeX font as your default font in Matplotlib, you can use this code

plt.rcParams['text.usetex'] = True

If you face some errors, you need to read the following story. I have explained the detailed procedures to deal with LaTeX font in Matplotlib

5 Powerful Tricks to Visualize Your Data with Matplotlib

To customize the other parameters (font size, font family, and tick params) you just write this code at the beginning of your code

plt.rcParams['font.size'] = 15
plt.rcParams['font.family'] = "serif"
tdir = 'in'
major = 5.0
minor = 3.0
plt.rcParams['xtick.direction'] = tdir
plt.rcParams['ytick.direction'] = tdir
plt.rcParams['xtick.major.size'] = major
plt.rcParams['xtick.minor.size'] = minor
plt.rcParams['ytick.major.size'] = major
plt.rcParams['ytick.minor.size'] = minor

If you need to understand it in more detail, you can visit this story

Create Professional Plots Using Matplotlib

01. Scatter plot

In this section, there are eight examples of scatter plots. Before creating a scatter plot, I need to generate mock data using this code

import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.linspace(0., 10., N)
y = np.sin(x)**2 + np.cos(x)

Variable x is an array of 50 data, from 0 to 10. Variable y is the summation of the square of sin(x) and cos(x). I want to visualize variable x in the x-axis, and variable y in the y-axis in the form of a scatter plot using this code

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

It is so simple. The code will show you a result, as shown in Figure 3.

Figure 3. Default scatter plot in Matplotlib (Image by Author).
Figure 3. Default scatter plot in Matplotlib (Image by Author).

To make it more beautiful, you can reduce the size of each data and give the label using this code

plt.scatter(x, y, s = 15, label = r'$y  = sin^2(x) + cos(x)$')

To change the color, you need to add this argument in scatter syntax

color = 'r' # r means red

If you want to make the axis scale is at the same scale, you can use this code

plt.axis('equal')

To create an axis label for the x- and y-axis, you can add the following code

plt.xlabel(r'$x$ (rad)')
plt.ylabel(r'$y$')

You have labeled your scatter plot, but you have not shown it as a legend. To show it, you can use this code

plt.legend()

To save your plot, you can use save figure syntax as shown in the following code

plt.savefig('scatter2.png', dpi = 300, bbox_inches = 'tight', facecolor='w')

The code above will save your plot with the name of scatter2.png, resolution of 300 dots per inch, tight bbox, and white background. It is okay if you omitted bbox_inches and face color arguments, but maybe you will get a different result. Just try it.

Here is the full code

The code will create a scatter plot, as shown in Figure 4.

Figure 4. Modified scatter plot in Matplotlib (Image by Author).
Figure 4. Modified scatter plot in Matplotlib (Image by Author).

You can see that the tick direction in the x-axis and y-axis inside the axes, and the font used is in LaTeX format. If you want to change the figure size, you can add figure size arguments inside plt.figure()

plt.figure(figsize=(7, 4.5))

Change marker style

To change the marker style, for example, I want to change from dot to cross, you can add this argument in plt.scatter

marker = 'x'

Figure 5 is the result if you apply the code above

Figure 5. Modify marker style in scatter plot with Matplotlib (Image by Author).
Figure 5. Modify marker style in scatter plot with Matplotlib (Image by Author).

There are tons of marker styles you can use in Matplotlib. You can check it at the following link.

matplotlib.markers – Matplotlib 3.3.2 documentation

If you have read the documentation above, you can realize that you can use the alphabet as your marker style. I will show you the example of applying the alphabet as a marker, as shown in Figure 6.

Figure 6. Alphabet marker in Matplotlib (Image by Author).
Figure 6. Alphabet marker in Matplotlib (Image by Author).

To generate Figure 6, I create a different function for the parameter in the x- and y-axis. Here is the code to generate it

np.random.seed(100)
N = 50
randx = np.random.random(N) * 100
randy = np.random.random(N) * 100

To visualize variable randx and randy, I run this code

plt.figure(figsize=(7, 6))
plt.scatter(randx, randy, marker=r'$beta$', s = 150, color = 'darkorange')
plt.axis('equal')
plt.xlabel('randx')
plt.ylabel('randy')
plt.tight_layout()

I use the Greek symbol beta as my marker style. You can change it with a different alphabet, such as a, B, C, d, or 1, 2, 3, etc.

Customizing the size for each data

This subsection will show you how to create a scatter plot with different sizes for each data, as shown in Figure 7.

Figure 7. Customize the size of the scatter plot in Matplotlib (Image by Author).
Figure 7. Customize the size of the scatter plot in Matplotlib (Image by Author).

To create it, I generate a random position for variable randx and randy, from 0 to 100 using this code

np.random.seed(100)
N = 30
randx = np.random.random(N) * 100
randy = np.random.random(N) * 100

For your reminder, I generate random data using Numpy. In generating a random number, Numpy only generates it in a range of 0 to 1. As I know, it is a convention in generating a random number (not only in Numpy), just from 0 to 1. To modify it, you can multiply it by 100. So, you will get a random number in the range of 0 to 100.

After that, I generate a random integer size for each data from 50 to 200, using this code

size = np.random.randint(50, 200, size=N)

To visualize it, you just add the argument of the size that will be applied in each data, using this code

plt.scatter(randx, randy, s = size, color = 'darkorange')

The additional syntax in creating Figure 7 is inserting the minor tick in the x- and y-axis. To insert it, you need to import submodule MultipleLocator using this code

from matplotlib.ticker import MultipleLocator

After that, you can add this syntax to insert minor axis

ax = plt.gca()
ax.xaxis.set_minor_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(10))

Here is the full code to generate Figure 7.

Color-coded scatter plot

You can change the color using colormaps. It means that data with different sizes will be color-coded by a different color. You can add color arguments in plt.scatter() like this

c = size

To embed the colorbar, you can use this code

plt.colorbar()

You will get a plot, as shown in Figure 8.

Figure 8. Modify color-coded in scatter plot with Matplotlib (Image by Author).
Figure 8. Modify color-coded in scatter plot with Matplotlib (Image by Author).

Here is the full code to generate Figure 8.

Customizing the colormaps

You can change the colormaps using this argument

cmap = 'inferno'

You can check this link to understand all of the colormaps provided by Matplotlib

Choosing Colormaps in Matplotlib – Matplotlib 3.3.2 documentation

In this tutorial, I have created my own colormaps by combining colormaps Blues and Oranges, as shown in Figure 9.

Figure 9. Customizing colormaps in Matplotlib (Image by Author).
Figure 9. Customizing colormaps in Matplotlib (Image by Author).

To combine it, I use this code

from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
top = cm.get_cmap('Oranges_r', 128)
bottom = cm.get_cmap('Blues', 128)
newcolors = np.vstack((top(np.linspace(0, 1, 128)),
                       bottom(np.linspace(0, 1, 128))))
orange_blue = ListedColormap(newcolors, name='OrangeBlue')

I create my own colormaps named _orange_blue_. To understand how to create and customize your own colormaps in Matplotlib, you can read it in the following link

Creating Colormaps in Matplotlib

To apply it, I just change color arguments _c = orange_blue_. You can check the result in Figure 11.

Figure 11. Customize colormaps in a scatter plot with Matplotlib (Image by Author).
Figure 11. Customize colormaps in a scatter plot with Matplotlib (Image by Author).

Here is the full code to generate Figure 11.

This the end of this section, creating a scatter plot with Matplotlib. If you face some errors, you can leave a comment in the response columns.

02. Line plot

To make a line plot in Matplotlib, I will generate mock data using this code

N = 50
x = np.linspace(0., 10., N)
y = np.sin(x)**2 + np.cos(x)

To visualize variable x and y in the form of a line plot, you need to use the following simple code

plt.plot(x, y)

The code above will generate a figure, as shown in Figure 12.

Figure 12. Default line plot in Matplotlib (Image by Author).
Figure 12. Default line plot in Matplotlib (Image by Author).

Customizing line styles

You can change the line style of the line plot in Matplotlib using this argument

linestyle = '-'

The argument above should be inserted in plt.plot() syntax. In this tutorial, I will show you four different line style; they are

['-', '--', '-.', ':']

To generate it automatically, I use looping to make it simple. Here is the full code

I will distribute the 4 different line styles in one figure. It means that I need to create 4 axes in a figure. In Matplotlib, you can generate it by customizing subplots using GridSpec(), subplot(), and add_subplot(). In this session, I use GridSpec() syntax. I create 4 axes (2 rows and 2 columns) with width and height space equals 0.25 (see lines 6 to 12). As I mentioned at the beginning, I will only focus on customizing plots. If you need more explanation in customizing subplots in Matplotlib, you can visit this link

Customizing Multiple Subplots in Matplotlib

If you run the code above, it will create a figure, as shown in Figure 13.

Figure 13. Customizing the line styles in Matplotlib with automation (Image by Author).
Figure 13. Customizing the line styles in Matplotlib with automation (Image by Author).

The code will simply generate 4 different line styles, with labels and annotations for each line style. Matplotlib provides many line styles you can use. You can choose your favorite line styles via this link

matplotlib.pyplot.plot – Matplotlib 3.3.2 documentation

Customizing line width

To customize the line width for your line plot, you can use this argument

lw = 2.0

I present you with 4 different line width, as shown in Figure 14.

Figure 14. Customizing the line width in Matplotlib (Image by Author).
Figure 14. Customizing the line width in Matplotlib (Image by Author).

I use a similar technique to create Figure 14. Here is the full code.

Creating mark every

This subsection will guide you to create a mark every feature. To understand it, I will show the result first, as shown in Figure 15.

Figure 15. Mark every in Matplotlib (Image by Author).
Figure 15. Mark every in Matplotlib (Image by Author).

In Figure 15, I create a circle mark for every 5 data. You can create it with this argument

'o'            # shape for each 5 data
markevery = 5  # mark every 
ms = 7         # size of the circle in mark every

Here is the full code

You need to place the argument ‘o’ in the third position.

Changing the line color

To change the line color, you can apply a similar concept to change the scatter color, as shown in the following code

color = 'royalblue'

I want to show you how to generate 4 different colors and 4 different marks using looping, as shown in Figure 16.

Figure 16. Customizing the line color in Matplotlib (Image by Author).
Figure 16. Customizing the line color in Matplotlib (Image by Author).

You can reproduce Figure 16 with this code

4 different colors I used are colorblind-friendly. I get it from this link. To understand how to create colorblind-friendly palettes using that link, you read this story.

Inserting error in line plot

To demonstrate an error bar in line plot, I need to generate the error, using this code

np.random.seed(100)
noise_x = np.random.random(N) * .2 + .1
noise_y = np.random.random(N) * .7 + .4

The code will generate random numbers from 0.1 to 0.3 for noise_x and from 0.3 to 0.7 for noise_y. To insert the error bar for the y-axis, you can use this code

plt.errorbar(x, y, yerr = noise_y)

Figure 17 is an example of error bar in line plot.

Figure 17. Creating an error bar plot in Matplotlib (Image by Author).
Figure 17. Creating an error bar plot in Matplotlib (Image by Author).

You can create Figure 17 with this code.

To insert error bar in the x-axis, you can use this argument

xerr = noise_x

You can see the example of inserting an error bar in the x- and y-axis in Figure 18.

Figure 18. Creating an error bar plot in Matplotlib (Image by Author).
Figure 18. Creating an error bar plot in Matplotlib (Image by Author).

You can use this code to reproduce Figure 18.

If you want to visualize your data without a line plot, only the error bar, you can use this argument

fmt = 'o'    # shape of the data point
color = 'r'  # color of the data point
ecolor ='k'   # color of the error bar

Here is the full code

The code above will show a plot, as shown in Figure 19.

Figure 19. Customizing the error bar in Matplotlib (Image by Author).
Figure 19. Customizing the error bar in Matplotlib (Image by Author).

Filling the error area

To visualize the error, you can also use this code

plt.fill_between(x, y + noise, y - noise, alpha = .5)

The fillbetween arguments are data for the x-axis, upper limit, and lower limit for the filled area. In the code above, it is represented by y + noise and y-noise_. You need to lower the transparency of the filled area. Here is the full code

If you run the code above, you will get a result, as shown in Figure 20.

Figure 20. Creating fill between the area in Matplotlib (Image by Author).
Figure 20. Creating fill between the area in Matplotlib (Image by Author).

Inserting the vertical and horizontal lines

You can insert a horizontal and a vertical line with this code

plt.hlines(0, xmin = 0, xmax = 10)
plt.vlines(2, ymin = -3, ymax = 3)

You need to define horizontal lines in the first argument, followed by the horizontal line’s starting point and ending point. For the vertical line, it has similar arguments.

Figure 21 is an example of inserting a horizontal and vertical line.

Figure 21. The horizontal and vertical line in Matplotlib (Image by Author).
Figure 21. The horizontal and vertical line in Matplotlib (Image by Author).

To generate Figure 21, you can use this code

In Figure 21, the legend is placed outside the axes. You can realize it in line 17.

Filling between two vertical lines

This subsection will guide you to make a filled area between two vertical lines, as shown in Figure 22.

Figure 22. Creating the filled area in Matplotlib (Image by Author).
Figure 22. Creating the filled area in Matplotlib (Image by Author).

To reproduce Figure 22, you can use this code

You need to know the important thing in creating a filled area in Matplotlib, you need to set a suitable y-axis limit.

03. Histogram

This section will explain how to make a histogram in 1D and 2D. I will tell you about the 1D histogram first. Before visualizing a 1D histogram, I will make a mock data, a Normal distributed random number, using this code.

N = 1000
np.random.seed(10021)
x = np.random.randn(N) * 2 + 15

In default, Numpy will generate a normal distributed random number with mean/median (mu) equals 0 and variance (sigma) equals 1. In the code above, I change mu to 15 and sigma to 2. To visualize variable x in 1D histogram, you can use this code

plt.hist(x)

The code will show a figure, as shown in Figure 23.

Figure 23. The default 1D histogram in Matplotlib (Image by Author).
Figure 23. The default 1D histogram in Matplotlib (Image by Author).

Matplotlib will generate 10 bins for a 1D histogram as the default setting. If you want to change the bins number, you can use this argument.

bins = 40

The argument will generate a 1D histogram with 40 bins, as shown in Figure 24.

Figure 24. Modify a 1D histogram in Matplotlib (Image by Author).
Figure 24. Modify a 1D histogram in Matplotlib (Image by Author).

Here is the full code to create Figure 24.

N = 1000
np.random.seed(10021)
x = np.random.randn(N) * 2 + 15
plt.figure(figsize=(9, 6))
plt.hist(x, bins = 40, label = r'$mu = 15, sigma = 2$')
plt.legend()

You can also limit the range of histogram using this argument

range = (12, 18)

The argument will let histogram only shows the data from 12 to 18, as shown in Figure 25.

Figure 25. Modify the data limit of the 1D histogram in Matplotlib (Image by Author).
Figure 25. Modify the data limit of the 1D histogram in Matplotlib (Image by Author).

You can reproduce Figure 25 with this code.

I also change the histogram color using a color argument.

Horizontal histogram

You can create a horizontal histogram, as shown in Figure 26.

Figure 25. A horizontal histogram in Matplotlib (Image by Author).
Figure 25. A horizontal histogram in Matplotlib (Image by Author).

You need to use this argument to create a horizontal histogram

orientation = 'horizontal'

To create Figure 25, you can run this code.

If you want to show the border from each histogram, you can use this argument.

edgecolor = 'k'

I want to make the histogram border in black color, as shown in Figure 26.

Figure 26. Customize histogram border in Matplotlib (Image by Author).
Figure 26. Customize histogram border in Matplotlib (Image by Author).

To generate Figure 26, you can use this full code.

Overlapping histogram

You can show many histograms in a single figure, as shown in Figure 27.

Figure 27. Create many histograms in Matplotlib (Image by Author).
Figure 27. Create many histograms in Matplotlib (Image by Author).

In Figure 27, I generate three normal distributions, with different mu and sigma. You can reproduce Figure 27 with this code.

You can make it prettier by changing the histograms’ transparency, as shown in Figure 28.

Figure 28. Change the transparency of the histograms in Matplotlib (Image by Author).
Figure 28. Change the transparency of the histograms in Matplotlib (Image by Author).

If you need the full code to create Figure 28, you can read the code below. The difference with the previous code is just in the alpha argument.

You can also generate Figure 28 using a looping, as shown in this code

N = 1000
mu1 = 5
mu2 = 10
mu3 = 15
sigma1 = 5
sigma2 = 3
sigma3 = 2
x1 = np.random.randn(N) * sigma1 + mu1
x2 = np.random.randn(N) * sigma2 + mu2
x3 = np.random.randn(N) * sigma3 + mu3
mu = np.array([mu1, mu2, mu3])
sigma = np.array([sigma1, sigma2, sigma3])
x = np.array([x1, x2, x3])
colors = ['royalblue', 'tomato', 'gray']
plt.figure(figsize=(9, 6))
for i in range(len(x)):
    plt.hist(x[i], bins = 30, color = colors[i], 
             label = r'$mu = $ ' + str(mu[i]) + 
             ', $sigma = $ ' + str(sigma[i]), alpha = .7)
plt.legend()

After see the code above, maybe you have an imagination to create a lot of histogram (more than 3) in a single figure. I will accommodate it :D. Here is the code to create and visualize 10 histograms in a single figure cleanly.

If you run the code above, you will get a result, as shown in Figure 29.

Figure 29. Create many histograms in Matplotlib cleanly (Image by Author).
Figure 29. Create many histograms in Matplotlib cleanly (Image by Author).

You can generate many colors merely using this link. After generating the palettes, just copy and paste it into your code. The detailed procedure of generating the palettes is provided here.

2D histogram

You can generate a 2D histogram with Matplotlib, as shown in Figure 30.

Figure 30. 2D histogram with Matplotlib (Image by Author).
Figure 30. 2D histogram with Matplotlib (Image by Author).

To create Figure 30, I need to generate 2 normal distribution with this code.

N = 1_000
np.random.seed(100)
x = np.random.randn(N)
y = np.random.randn(N)

To visualize variable x and y in the 2D histogram, you can use this code.

plt.hist2d(x, y)

As in the 1D histogram, Matplotlib will generate 10 bins as the default setting for the 2D histogram. To change it, you can apply the same argument as in the 1D histogram, as shown in the code below.

bins = (25, 25)

You can see the modified bins number of the 2D histogram in Figure 31.

Figure 31. Modifying bins number of the 2D histogram in Matplotlib (Image by Author).
Figure 31. Modifying bins number of the 2D histogram in Matplotlib (Image by Author).

You also can change the colormaps of your 2D histogram using this argument

cmap = orange_blue

I want to change the colormaps from Viridis (default colormaps from Matplotlib) to my own colormaps named orange_blue. I have explained how to create your own colormaps in the previous section, or you can read it here.

Here is the full code for modifying the colormaps used in the 2D histogram.

If you run the code above, it will create a figure, as shown in Figure 32.

Figure 32. Change the colormaps of a 2D histogram in Matplotlib (Image by Author).
Figure 32. Change the colormaps of a 2D histogram in Matplotlib (Image by Author).

You can limit the range of counts for each (change the limit of the colorbar) by applying this argument into plt.hist2d().

cmin = 5, cmax = 25

Here is the full code

N = 10_000
np.random.seed(100)
x = np.random.randn(N)
y = np.random.randn(N)
plt.figure(figsize=(8.5, 7))
plt.hist2d(x, y, bins=(75, 75), cmap = 'jet', cmin = 5, cmax = 25)
cb = plt.colorbar()
cb.set_label('counts each bin', labelpad = 10)

I use ‘jet’ colormaps with the lower limit for the colorbar equlas 5 and for the upper limit is 25. The code will generate a figure, as shown in Figure 33.

Figure 33. Set the limit of a 2D histogram in Matplotlib (Image by Author).
Figure 33. Set the limit of a 2D histogram in Matplotlib (Image by Author).

I try to change the generated random number counts from 10000 to 100000 using this code.

N = 100_000
np.random.seed(100)
x = np.random.randn(N)
y = np.random.randn(N)
plt.figure(figsize=(8.5, 7))
plt.hist2d(x, y, bins=(75, 75), cmap = 'Spectral')
cb = plt.colorbar()
cb.set_label('counts each bin', labelpad = 10)

The code will show a result, as shown in Figure 34.

Figure 34. Visualize the normal distribution in the 2D histogram with Matplotlib (Image by Author).
Figure 34. Visualize the normal distribution in the 2D histogram with Matplotlib (Image by Author).

Figure 34 peaked at around 0 and distributed around -1 to 1 because I did not change the value of mu and sigma.

Marginal plot

This subsection will tell how to create a marginal distribution, as shown in Figure 35.

Figure 35. The marginal plot from scatter distribution and histogram in Matplotlib (Image by Author).
Figure 35. The marginal plot from scatter distribution and histogram in Matplotlib (Image by Author).

Figure 35 is built by scatter plot and 2 histogram. To create it, you need to understand how to customize the subplots or axes in a single figure. Figure 35 is constructed by 25 axes (5 columns and 5 rows). The detailed is shown in Figure 36. You can create Figure 36 with this code. You need to read this link to understand it.

rows = 5
columns = 5
grid = plt.GridSpec(rows, columns, wspace = .4, hspace = .4)
plt.figure(figsize=(10, 10))
for i in range(rows * columns):
    plt.subplot(grid[i])   
    plt.annotate('grid '+ str(i), xy = (.5, .5), ha = 'center', 
                 va = 'center')
for i in range(rows):
    exec (f"plt.subplot(grid[{i}, 0])")
    plt.ylabel('rows ' + str(i), labelpad = 15)
for i in range(columns):
    exec (f"plt.subplot(grid[-1, {i}])")
    plt.xlabel('column ' + str(i), labelpad = 15)
Figure 36. Multiple subplots in Matplotlib (Image by Author).
Figure 36. Multiple subplots in Matplotlib (Image by Author).

Figure 35 shows the transformation of Figure 36. I merge some grids in Figure 36 to only 3 bigger grids. The first grid combines grids 0 to grids 3 (rows 1, column 0 to column 3). I will fill the first grid with the histogram plot. The second grid merges 16 grids from rows 1 to rows 4 and from column 0 to column 3. The last grid is built by combining grids 9, 14, 19, and 24 (rows 1, 2, 3, 4, and column 4).

To create the first grid, you can use this code.

rows = 5
columns = 5
grid = plt.GridSpec(rows, columns, wspace = .4, hspace = .4)
plt.figure(figsize=(10, 10))
plt.subplot(grid[0, 0:-1])

After that, add the code below to insert 1D histogram

plt.hist(x, bins = 30, color = 'royalblue', alpha = .7)

To create the second grid, you can add this code to the code above

plt.subplot(grid[1:rows+1, 0:-1])

Add this code to generate scatter plot in the second grid.

plt.scatter(x, y, color = 'royalblue', s = 10)
plt.axis('equal')

Here is the code to generate the third grid and its histogram. You need to insert the code below into the first grid code

plt.subplot(grid[1:rows+1, -1])
plt.hist(y, bins = 30, orientation='horizontal', 
         color = 'royalblue', alpha = .7)

You can combine it all, as shown in the following code.

Next, I will change the scatter plot in the second grid with the 2D histogram, as shown in Figure 37.

Figure 37. The marginal plot in Matplotlib (Image by Author).
Figure 37. The marginal plot in Matplotlib (Image by Author).

You can reproduce Figure 37 with this code.

Please leave some comments in the response column if you meet some errors.

04. Bar chart

If you want to visualize your data with a bar chart, this is suitable for you. Before I create a bar chart in Matplotlib, as usual, I want to create the mock data to be shown. I want to create data in the Math exam score for six persons. To create it, I use the following code.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
np.random.seed(100)
N = len(name)
math = np.random.randint(60, 100, N)

I generate the Math exam scores from 60 to 100. To visualize it, you can use this code.

plt.bar(name, math, alpha = .7)

After adding some information, I generate a bar chart, as shown in Figure 38.

Figure 38. Create a bar chart in Matplotlib (Image by Author).
Figure 38. Create a bar chart in Matplotlib (Image by Author).

Here is the full code to generate Figure 38.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
np.random.seed(100)
N = len(name)
math = np.random.randint(60, 100, N)
plt.figure(figsize=(9, 6))
plt.bar(name, math, alpha = .7)
plt.ylabel('Math Exam')

After that, I create some more mock data for Physics, Biology, and Chemistry exam scores using this code.

np.random.seed(100)
N = len(name)
math = np.random.randint(60, 100, N)
physics = np.random.randint(60, 100, N)
biology = np.random.randint(60, 100, N)
chemistry = np.random.randint(60, 100, N)

You can create a table, in Python we call it DataFrame, using Pandas. The DataFrame I create from the mock data is shown in Figure 39.

Figure 39. The mock data in Pandas DataFrame (Image by Author).
Figure 39. The mock data in Pandas DataFrame (Image by Author).

As a default, I did not insert the code on how to create the DataFrame. But, if you need it, you leave your request in the response column.

Then, I visualize it, as shown in Figure 40.

Figure 40. Create many bar charts in Matplotlib (Image by Author).
Figure 40. Create many bar charts in Matplotlib (Image by Author).

To create Figure 40, you can use this code.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
np.random.seed(100)
N = len(name)
math = np.random.randint(60, 100, N)
physics = np.random.randint(60, 100, N)
biology = np.random.randint(60, 100, N)
chemistry = np.random.randint(60, 100, N)
rows = 2
columns = 2
plt.figure(figsize=(12, 8))
grid = plt.GridSpec(rows, columns, wspace = .25, hspace = .25)
plt.subplot(grid[0])
plt.bar(name, math, alpha = .7)
plt.ylabel('Math Exam')
plt.ylim(60, 100)
plt.subplot(grid[1])
plt.bar(name, physics, alpha = .7)
plt.ylabel('Physics Exam')
plt.ylim(60, 100)
plt.subplot(grid[2])
plt.bar(name, biology, alpha = .7)
plt.ylabel('Biology Exam')
plt.ylim(60, 100)
plt.subplot(grid[3])
plt.bar(name, chemistry, alpha = .7)
plt.ylabel('Chemistry Exam')
plt.ylim(60, 100)

or use this code if you want use a looping.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
course_name = ['Math', 'Physics', 'Biology', 'Chemistry']
N = len(name)
rows = 2
columns = 2
plt.figure(figsize=(12, 8))
grid = plt.GridSpec(rows, columns, wspace = .25, hspace = .25)
for i in range(len(course_name)):
    np.random.seed(100)
    course = np.random.randint(60, 100, N)
    plt.subplot(grid[i])
    plt.bar(name, course, alpha = .7)
    plt.ylabel(course_name[i] + ' Exam')
    plt.ylim(60, 100)

Horizontal bar chart

You can use a horizontal bar chart with this code.

plt.barh(name, course)

I want to present Figure 40 in the form of horizontal bar chart and in various colors. Here is the full code to generate it.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
course_name = ['Math', 'Physics', 'Biology', 'Chemistry']
colors = ['#00429d', '#7f40a2', '#a653a1', '#c76a9f', 
          '#e4849c', '#d0e848']
N = len(name)
rows = 2
columns = 2
plt.figure(figsize=(12, 8))
grid = plt.GridSpec(rows, columns, wspace = .25, hspace = .25)
for i in range(len(course_name)):
    np.random.seed(100)
    course = np.random.randint(60, 100, N)
    plt.subplot(grid[i])
    plt.barh(name, course, color = colors)
    plt.xlabel(course_name[i] + ' Exam')
    plt.xlim(60, 100)
    plt.gca().invert_yaxis()

After you run the code above, you will get a result, as shown in Figure 41.

Figure 41. Horizontal bar chart in Matplotlib (Image by Author).
Figure 41. Horizontal bar chart in Matplotlib (Image by Author).

You can insert the error bar in the horizontal bar chart using this argument

N = len(name)
noise = np.random.randint(1, 3, N)
plt.barh(name, course, xerr = noise)

I create the error using integer random number from 1 to 3, as mentioned in the variable noise. After adding some elements for my horizontal bar chart, I show it, as shown in Figure 42.

Figure 42. Modifying the horizontal bar chart in Matplotlib (Image by Author).
Figure 42. Modifying the horizontal bar chart in Matplotlib (Image by Author).

You can reproduce Figure 42 with this code.

name = ['Adam', 'Barry', 'Corbin', 'Doe', 'Evans', 'Frans']
course_name = ['Math', 'Physics', 'Biology', 'Chemistry']
N = len(name)
rows = 2
columns = 2
plt.figure(figsize=(12, 8))
grid = plt.GridSpec(rows, columns, wspace = .25, hspace = .25)
np.random.seed(100)
for i in range(len(course_name)):
    course = np.random.randint(60, 95, N)
    noise = np.random.randint(1, 3, N)
    plt.subplot(grid[i])
    plt.barh(name, course, color = colors, xerr = noise, 
             ecolor = 'k')
    plt.xlabel(course_name[i] + ' Exam')
    plt.xlim(60, 100)
    plt.gca().invert_yaxis()

Maybe, you realize that my mock data (with error) is not realistic. You can not meet an exam score with the uncertainty. But, I think it is still a good example in understanding the bar chart in Matplotlib.

Conclusion

This is the end of part 1. As I mentioned before, I try to limit the reading time (less than 30 minutes), so you can enjoy the reading. This part only covers 4 from 11 sections, scatter plot, line plot, histogram, and bar chart. In the next part, I will show the tutorials to create a box plot, violin plot, pie chart, polar chart, geographic projection, 3D plot, and contour plot. If the next part is consuming more than 30 minutes, I will divide it again.

Data visualization is significant in analyzing data from small data or big data in the technological era. We need it to have a global picture of our data. Various types of visualization you can use with Matplotlib. This is just a small part of python plotting with Matplotlib.

If you liked this article, here are some other articles you may enjoy:

5 Powerful Tricks to Visualize Your Data with Matplotlib

Matplotlib Styles for Scientific Plotting

Creating Colormaps in Matplotlib

Customizing Multiple Subplots in Matplotlib

Introduction to Big Data with Vaex – A Simple Code to Read 1.25 Billion Rows

That’s all. Thanks for reading this story. Comment and share if you like it. I also recommend you follow my account to get a notification when I post my new story. In a couple of days, I will publish Part 2 for Visualizations with Matplotlib.


Related Articles