English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib subplot2grid() function

Matplotlib subplot2The grid() function provides greater flexibility in creating axes objects at specific positions in the grid. It also allows axes objects to span multiple rows or columns.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
plt.subplot2grid(shape, location, rowspan, colspan)

In the following example, the graph object's3X3The grid fills the axes of different sizes in the span of rows and columns, each displaying a different graph.

Refer to the following example code:

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 # Display Chinese settings...
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Step two (solve the problem of displaying negative signs on the axis)
 a1 = plt.subplot2grid((3,3),(0,0), colspan = 2)
 a2 = plt.subplot2grid((3,3),(0,2), rowspan = 3)
 a3 = plt.subplot2grid((3,3),(1, rowspan = 2, colspan = 2)
 x = np.arange(1,10)
 a2.plot(x, x*x)
 a2.set_title('Square')
 a1.plot(x, np.exp(x))
 a1.set_title('Exponential')
 a3.plot(x, np.log(x))
 a3.set_title('log')
 plt.tight_layout()
 plt.show()

Execute the above example code to get the following results -