Matplotlib Tutorial — 10

Vivekawasthi
CodeX
Published in
3 min readDec 18, 2022

This tutorial will cover subplots using matplotlib.

The subplots() function in the pyplot module of the matplotlib library is used to create a figure and a set of subplots.

let’s check the basic line plot we created in the previous blog first, then we will convert the same in the subplots.

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('subplotData.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']

plt.plot(ages, py_salaries, label='Python')
plt.plot(ages, js_salaries, label='JavaScript')

plt.plot(ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')

plt.legend()

plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')

plt.tight_layout()

plt.show()

Here, we have imported dev data from CSV and plotted a line chart for Python, and Javascript, and all devs we will run this code will get a simple line chart for the same.

Now, we have a requirement to plot this graph in such a way where Python and javascript lines will come in one figure and all devs will come in another, which we will perform using the subplot() method.

subplots() without arguments return a Figure and a single Axes.

This is actually the simplest and recommended way of creating a single Figure and Axes.

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')

Now, create an object of a subplot in our code and create the same graph using subplot().

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('subplotData.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']

fig , ax = plt.subplots()
ax.plot(ages, py_salaries, label='Python')
ax.plot(ages, js_salaries, label='JavaScript')

ax.plot(ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')

ax.legend()

ax.set_title('Median Salary (USD) by Age')
ax.set_xlabel('Ages')
ax.set_ylabel('Median Salary (USD)')

plt.tight_layout()

plt.show()

Here, we have created an object of subplots method without any argument, so it will return one figure and one axis, also instead of plt.plot() we will replace the same using ax.plot() .

The same is for the legend as well, for the title label it got changed to set_title and xlabel and ylabel when we used the subplots, so when we run the same code we will get the same graph but using the subplots() method.

Now, back to our original requirement, where we want to create Python and Java scripts in one axis and all devs in another.

To achieve the same we have to pass the argument as two rows and one column as an argument.

fig , (ax1,ax2) = plt.subplots(nrows=2,ncols=1)

Since it is returning two axes now, so we will unpack here and create ax1 and ax2 for two axes, ax1 will use them for Python and JS and ax2 for all devs, similarly, we will create titles and axes separately as well for both the figures.

import pandas as pd
from matplotlib import pyplot as plt

plt.style.use('seaborn')

data = pd.read_csv('subplotData.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']

fig , (ax1,ax2) = plt.subplots(nrows=2,ncols=1)
ax1.plot(ages, py_salaries, label='Python')
ax1.plot(ages, js_salaries, label='JavaScript')

ax2.plot(ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')

ax1.legend()

ax1.set_title('Median Salary (USD) by Age for Python , JS')
ax1.set_xlabel('Ages')
ax1.set_ylabel('Median Salary (USD)')

ax2.legend()

ax2.set_title('Median Salary (USD) by Age for All devs')
ax2.set_xlabel('Ages')
ax2.set_ylabel('Median Salary (USD)')


plt.tight_layout()

plt.show()

Now instead of one figure, if we want to split those graphs into two figures we can simply perform just creating two objects for the same.

fig1 ,ax1 = plt.subplots()
fig2 , ax2 = plt.subplots()

The rest of the code remains the same, and when we will run the code, we will get two figures instead of one.

Below is the link for the code and datasheet used in this tutorial.

--

--