English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Matplotlib's pyplot API has a convenient function called subplots(), which acts as a utility wrapper and helps create a common layout for subplots in a single call, including a closed graphics object. The prototype of the function is as follows:
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 plt.subplots(nrows, ncols)
This function takes two integer parameters specifying the number of rows and columns of the subplot grid. The function returns a graphics object and a list containing an equal number of rows * ncols tuple of axis objects. Each axis object can be accessed by index. Here, we create a2rows2subplots of columns, and display4different figures.
Refer to the following implementation 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 of negative numbers on the coordinate axis) fig, a = plt.subplots(2,2) x = np.arange(1,5) a[0][0].plot(x, x*x) a[0][0].set_title('Square') a[0][1].plot(x, np.sqrt(x)) a[0][1].set_title('Square Root') a[1][0].plot(x, np.exp(x)) a[1][0].set_title('Exponential') a[1][1].plot(x, np.log10(x)) a[1][1].set_title('log') plt.show()
Execute the above example code to get the following result -