English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this chapter, we will learn how to create multiple subplots on the same canvas.
The subplot() function returns an axes object at the given grid position. The signature of this function is -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 plt.subplot(subplot(nrows, ncols, index))
In the current graph, this function creates and returns an Axes object at the grid position index of ncolsaxes in nrows. The index starts from1to nrows * ncols, increasing sequentially by rows. If nrows, ncols, and index are all less than10The index can also be given as a single, connected, or three-digit number.
For example, subplot(2, 3, 3) and subplot(233) are created in the upper right corner of the current graph, occupying half of the graph height and one-third of the graph width.
Creating a subplot will delete any pre-existing subplot that overlaps it, rather than sharing boundaries.
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 # Display Chinese settings... plt.rcParams['font.sans-serif'] = ['SimHei'] # Step one (replacement of sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Step two (solve the problem of displaying the minus sign of negative numbers on the coordinate axis) Original from [Lidi Huo], please contact the author for commercial转载 authorization, retain the original link for non-commercial use # Plot a line, implicitly creating a subplot(111) plt.plot([1,2,3)) # Now create a subplot which represents the top plot of a grid with 2 rows and 1 column. # Since this subplot will overlap the first, the plot (and its axes) previously created will be removed plt.subplot(211) plt.plot(range(12)) plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background plt.plot(range(12)) plt.show()
Execute the above example code to get the following results:
The add_subplot() function of the figure class does not overwrite the existing graph, refer to the following 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 # Display Chinese settings... plt.rcParams['font.sans-serif'] = ['SimHei'] # Step one (replacement of sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Step two (solve the problem of displaying negative signs on the axis) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot([1,2,3)) ax2 = fig.add_subplot(221, facecolor='y') ax2.plot([1,2,3)) plt.show()
Execute the above example code to get the following results:
An insertion figure can be added to the same graph by adding another axis object on the same graph canvas. 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 (replacement of sans-serif font) plt.rcParams['axes.unicode_minus'] = False # Step two (solve the problem of displaying negative signs on the axis) x = np.arange(0, math.pi*2, 0.05) fig = plt.figure() axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8)) # main axes axes2 = fig.add_axes([0.55, 0.55, 0.3, 0.3)) # inset axes y = np.sin(x) axes1.plot(x, y, 'b') axes2.plot(x, np.cos(x), 'r') axes1.set_title('Sine') axes2.set_title("Cosine") plt.show()
Execute the above example code to get the following results: