English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In questa sezione, impareremo come creare più sottopannelli sullo stesso tavolo di disegno.
La funzione subplot() restituisce l'oggetto axes nella posizione della griglia data. La firma di questa funzione è -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : it.oldtoolbag.com # Date : 2020-08-08 plt.subplot(subplot(nrows, ncols, index))
Nell'attuale grafico, questa funzione crea e restituisce un oggetto Axes nella posizione dell'indice della griglia ncolsaxes * nrows. L'indice aumenta in ordine crescente da 1 a nrows * ncols, in ordine row-major. Se nrows, ncols e index sono tutti inferiori a 10. L'indice può anche essere fornito come singolo, connesso, o tre numeri.
Ad esempio, subplot(2, 3, 3) e subplot(233) creano entrambi un asse nell'angolo in alto a destra del grafico, occupando metà dell'altezza del grafico e un terzo della larghezza.
Creare un sottopannello eliminerà qualsiasi sottopannello preesistente sovrapposto, non condividendo i confini.
Riferimento al seguente esempio di codice:
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : it.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt # 显示中文设置... plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体) plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)原文出自【立地货】,商业转载请联系作者获得授权,非商业请保留原文链接 # 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()
Esegui il codice di esempio sopra per ottenere i seguenti risultati:
figure类的add_subplot()函数不会覆盖现有的图,参考以下代码 -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : it.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt # 显示中文设置... plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体) plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题) 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()
Esegui il codice di esempio sopra per ottenere i seguenti risultati:
可以通过在同一图形画布中添加另一个轴对象来在同一图中添加插入图。参考以下实现代码 -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : it.oldtoolbag.com # Date : 2020-08-08 #! /usr/bin/env python #coding=utf-8 import matplotlib.pyplot as plt import numpy as np import math # 显示中文设置... plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体) plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题) x = np.arange(0, math.pi*2, 0.05) fig = plt.figure() axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # asse principale axes2 = fig.add_axes([0.55, 0.55, 0.3, 0.3]) # asse insetto 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()
Esegui il codice di esempio sopra per ottenere i seguenti risultati: