
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adjusting the heights of individual subplots in Matplotlib in Python
The matplotlib is a library in Python that allows us to create graphs and plots for data visualization. This library provides several functions to adjust the height, width, and other layout properties of individual subplots in a figure. In this article, we will learn how to adjust the heights of individual subplots in matplotlib using Python.
- Subplots in Matplotlib
- Adjusting Heights of Subplots Using gridspec_kw
- Adjusting Heights of Subplots Using GridSpec
- Conclusion
Subplots in Matplotlib
A subplot is a smaller plot within a larger figure. For example, if we have a figure with three rows and two column, we can create six subplots inside the figure. The image below shows an example of a figure with four subplots arranged in a 3x2 grid:

To adjust the heights of individual subplots in matplotlib, there are two main methods:
We will discuss both methods with examples in the following sections.
Adjusting Heights of Subplots Using gridspec_kw
The subplot() function of matplotlib is used to create subplots for data visualization. It allows us to specify the number of rows and columns of subplots, as well as their sizes. If we add gridspec_kw parameter, we can adjust the heights of individual subplots in a figure. Following is the syntax to use subplot function with gridspec_kw parameter:
Syntax
fig, (ax1, ax2) = plt.subplots( 2, 1, # Create a figure with 2 rows and 1 column figsize=(8, 6), # Set figure size: 8 inches wide, 6 inches tall gridspec_kw={'height_ratios': [3, 1]} # ax1 is 3x the height of ax2 )
In this code, we create a figure fig with two subplots ax1 and ax2. The gridspec_kw parameter specifies the height ratios of the subplots. In this case, ax1 is set to be three times the height of ax2. Now, let's see a working example of this method.
Example
In the example below, we create a figure with two subplots and adjust their heights using the gridspec_kw parameter:
import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots( 2, 1, figsize=(8, 6), gridspec_kw={'height_ratios': [3, 1]} # ax1 is 3x the height of ax2 ) ax1.plot([1, 2, 3], [4, 5, 6]) ax1.set_title("Larger Subplot") ax2.plot([1, 2, 3], [1, 0, 1]) ax2.set_title("Smaller Subplot") plt.tight_layout() plt.show()
The output of above code will be:

Adjusting Heights of Subplots Using GridSpec
The GridSpec class is used to create a grid layout for subplots in matplotlib. It allows us to specify the number of rows, columns, and the height ratios of individual subplots. This method provides more control over the subplot layout than the above method.
Here is the syntax for adjusting the heights of individual subplots using GridSpec class:
Syntax
# Figure, 8 inches wide, 6 inches tall fig = plt.figure(figsize=(8, 6)) # Create a 3-row by 1-column grid layout gs = gridspec.GridSpec(3, 1, height_ratios=[2, 1, 1]) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1]) ax3 = fig.add_subplot(gs[2])
In this code, first we created a figure fig of size 8 inches wide and 6 inches tall. Then, we created a gridSpec object gs with 3 rows and 1 column, such that the heights of the subplots are in the ratio of 2:1:1.
Finally, we added three subplots ax1, ax2, and ax3 to the figure using the add_subplot method. Now, let's see a working example of this method.
Example
In the example below, we create a figure with two subplots and adjust their heights using the GridSpec class:
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure(figsize=(8, 6)) gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) # ax1 is 3x the height of ax2 ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1]) ax1.plot([1, 2, 3], [4, 5, 6]) ax1.set_title("Larger Subplot") ax2.plot([1, 2, 3], [1, 0, 1]) ax2.set_title("Smaller Subplot") plt.tight_layout() plt.show()
The output of the above code will be:

Conclusion
In this article, we learned how to adjust the heights of individual subplots in matplotlib using two methods: gridspec_kw and GridSpec. Both methods allow us to create subplots with different heights, providing flexibility in data visualization. You can choose the method that best suits your needs based on the complexity of your subplot arrangement.