plt.figure创建一张画布,使用plt.subplot函数或其他的作图函数在这张画布上作一幅图。如果要作多幅并列的图,使用plt.add_subplot函数。 plt.rcParams['font.family'] = 'Microsoft YaHei' fig1 = plt.figure(figsize=[8,7]) # ax1 = fig1.add_subplot(121) #1行2列,第1幅图 ax1.plot(x, y1) #pl...
plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background plt.plot([4,6,8]) plt.show() 1. 2. 3. 4. 5. 6. 7. 8. 9. 显示结果如下: 画布中的add_subplot()函数不会覆盖现有的图,看下面实例: import matplotlib.pyplot as plt fig = plt.figure() # 在这个画布中...
importnumpy as npimportmatplotlib.pyplot as plt x= np.arange(0, 100)#新建figure对象fig=plt.figure()#新建子图1ax1=fig.add_subplot(2,2,1) ax1.plot(x, x)#新建子图3ax3=fig.add_subplot(2,2,3) ax3.plot(x, x** 2) ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)#新...
add_subplot()方法的网格参数和索引 下面的代码就在figure中创建了一个2*2的网格,并在第4个格子中创建了一个axes坐标系。 from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure from matplotlib.axes import Axes import numpy as np from numpy import math fig =Figu...
import matplotlib.pyplot as plt import inspect plt.figure() #创建图例 <Figure size 432x288 with 0 Axes> 默认创建一个大小为432x288大小的画板(单位是像素)如果我们定义尺寸则需要使用英寸单位,1英寸…
add_subplot()是Figure对象的方法,用于在特定的Figure是那个添加子图。使用add_subplot()方法时,首先需要创建一个Figure对象,然后调用该方法来添加子图,并将子图对象存储在变量中以进行后续操作。 subplot()是pyplot模块的函数,用于在当前的图形中添加子图。subplot()语法为plt.subplot(nrows, ncols, index)。使用subpl...
fig = plt.figure()# 创建2x1网格的第一个子图 ax1 = fig.add_subplot(2, 1, 1)ax1.plot(x, y1)# 创建2x1网格的第二个子图 ax2 = fig.add_subplot(2, 1, 2)ax2.plot(x, y2)plt.show()这段代码在一个图形中创建了两个垂直排列的子图。使用subplots subplots函数是一个更简洁的方式,它一次...
使用plt.subplots()函数创建了一个figure对象和一个axes对象的数组。axs[0, 0]表示第一个子图,axs[0, 1]表示第二个子图,以此类推。在每个子图上使用不同的绘图函数绘制了不同的图形,并设置了标题和坐标轴标签。最后使用plt.show()函数显示图形。实例2:使用add_subplot在给定的轴对象上绘制子图 import ...
import matplotlib.pyplot as plt import numpy as np # 创建数据 x = np.linspace(0, 2 * np.pi, 100) y1 = np.sin(x) y2 = np.cos(x) y3 = np.sin(x) + np.cos(x) y4 = np.sin(x) - np.cos(x) # 创建一个图形对象 fig = plt.figure() # 添加子图 ax1 = fig.add_subplot(...
Figure 图 Axes 坐标轴(实际画图的地方) 注意,pyplot的方式中plt.subplot()参数和面向对象中的add_subplot()参数和含义都相同。 使用面向对象的方式 #!/usr/bin/python#coding: utf-8import numpy as np import matplotlib.pyplot as plt x = np.arange(0,100) ...