Subplots in Python with Matplotlib
By: Iroshan Aberathne
Tags: Graph, Data Science, Matplotlib, Python

In this article we are focusing on graphical representation of subplots with matplotlib library in python.

Data set Preparation

Three lists containing integers and floating point numbers are initialized as the data sets for graphs. The first list represents the x-axis ticks while second and third list are being used as the data points in subplots.

x_ticks = [1,2,3,4,5,6,7,8,9,10]

graph_one = [10000,10000,3000,4000,6000,6000,5000,5000,5000,10000]

graph_two = [25.45,1.31,0.1,3.08,1.6,0.15,0.2,0.08,7.04,5.99]

Two Horizontal Graphs

Firs we illustrate the steps to draw two distinct graphs in horizontally in one.

Set Height of the Graphs

Following two statements set the heights of the respective graphs.

heights_graph_one = [x for x, _ in enumerate(x_ticks)]

heights_graph_two = [x for x, _ in enumerate(graph_one)]

Plotting Graphs

Plotting two horizontal graphs.

import numpy as np

import matplotlib.pyplot as plt x_ticks = [1,2,3,4,5,6,7,8,9,10]

graph_one = [10000,10000,3000,4000,6000,6000,5000,5000,5000,10000]

graph_two = [25.45,1.31,0.1,3.08,1.6,0.15,0.2,0.08,7.04,5.99]



fig = plt.figure(figsize=(25, 15))

heights_graph_one = [x for x, _ in enumerate(x_ticks)]

heights_graph_two = [x for x, _ in enumerate(graph_one)]



# -----------------------------------------------------------------

plt.subplot(211)

plt.bar(heights_graph_one, graph_one, color='blue', edgecolor='black')

plt.xticks(np.arange(0, len(graph_one), 1), rotation="60")

plt.ylabel("Graph One", fontsize=12)

plt.title("Graph One", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(212)

plt.plot(heights_graph_two, graph_two, marker='o', color='red')

plt.xticks(np.arange(0, len(graph_two), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph Two", fontsize=12)



plt.show()

Fig.1. Horizontal Graphs

2. Plotting two vertical graphs.

You just need change only two statement in order to arrange graphs vertically.

plt.subplot(121) # previously plt.subplot(211)

plt.subplot(122) # previously plt.subplot(212)

Fig.2. Vertical Graphs

3. Plotting Three Graphs

The way of arranging three graphs in one plotting.

# ------------------------------------------------------------------

plt.subplot(212)

plt.plot(heights_graph_two, graph_two, marker='o', color='red')

plt.xticks(np.arange(0, len(graph_two), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph One", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(221)

plt.bar(heights_graph_one, graph_one, color='blue', edgecolor='black')

plt.xticks(np.arange(0, len(graph_one), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph Two", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(222)

plt.plot(heights_graph_two, graph_two, marker='o', color='red')

plt.xticks(np.arange(0, len(graph_two), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph Three", fontsize=12)

Fig.3. Plotting Three Graphs

4. Plotting Four Graphs

# ------------------------------------------------------------------

plt.subplot(221)

plt.bar(heights_graph_one, graph_one, color='blue', edgecolor='black')

plt.xticks(np.arange(0, len(graph_one), 1), rotation="60")

plt.ylabel("Value", fontsize=12)

plt.title("Graph One", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(222)

plt.plot(heights_graph_two, graph_two, marker='o', color='red')

plt.xticks(np.arange(0, len(graph_two), 1), rotation="60")

plt.ylabel("Value", fontsize=12)

plt.title("Graph Two", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(223)

plt.plot(heights_graph_two, graph_two, marker='o', color='red')

plt.xticks(np.arange(0, len(graph_two), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph Three", fontsize=12)



# ------------------------------------------------------------------

plt.subplot(224)

plt.bar(heights_graph_one, graph_one, color='blue', edgecolor='black')

plt.xticks(np.arange(0, len(graph_one), 1), rotation="60")

plt.xlabel("Sample", fontsize=12)

plt.ylabel("Value", fontsize=12)

plt.title("Graph Four", fontsize=12)

Fig.4.Four Graphs

This simple example shows the way of plotting subplots using mathplotlib in python.

Have a nice day!!