ax2.set_title('Second subplot')# 添加第三个子图 ax3 = fig.add_subplot(2, 2, 3)ax3.plot(y1, y2)ax3.set_title('Third subplot')# 添加第四个子图 ax4 = fig.add_subplot(2, 2, 4)ax4.plot(x, x2)ax4.set_title('Fourth subplot')plt.tight_layout()plt.show()以上两种方法都可以有效...
subplot是为了在一张图里放多个子图,与Matlab里的subplot类似。 pyplot是一个有状态的对象,包含了当前的图,画图区域,等。 pyplot通过调用subplot或者add_subplot来增加子图, 如 p1 = plt.subplot(211) 或者 p1 = plt.subplot(2,1,1), 表示创建一个2行,1列的图,p1为第一个子图, 然后在p1上画曲线,设置标...
0].plot(x,np.exp(x),label='Exp')axs[1,1].plot(x,np.log(x),label='Log')foriinrange(2):forjinrange(2):axs[i,j].set_title(f'Subplot{i+1},{j+1}')axs[i,j].legend()plt.suptitle('Adjusted
ax1 = fig.add_subplot(3, 1, 1) ax1.plot(x, y1) ax1.set_title('Sine') 创建第二个子图 ax2 = fig.add_subplot(3, 1, 2) ax2.plot(x, y2) ax2.set_title('Cosine') 创建第三个子图 ax3 = fig.add_subplot(3, 1, 3) ax3.plot(x, y3) ax3.set_title('Tangent') plt.tight_l...
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) x=[1,2,3,4,5,6,7,8,9,10] y=[1,1,1,2,10,2,1,1,1,1] line, = ax.plot(x, y) ymax = max(y) xpos = y.index(ymax) xmax = x[xpos] ax.annotate('local max', xy...
在这个例子中,最后一个子图跨越了底部的三个位置。 3.2 使用add_subplot()方法 除了pyplot.subplot()函数,我们还可以使用Figure对象的add_subplot()方法来创建子图: importmatplotlib.pyplotaspltimportnumpyasnp fig=plt.figure(figsize=(10,6))x=np.linspace(0,...
ax= fig.add_subplot(121) ax2= fig.add_subplot(122) ax.set_title('图表标题') ax.axis([-1,6,-2,2])#坐标范围,X轴-1到6,Y轴-2到2ax.set_xlabel('X轴标注') ax.set_ylabel('Y轴标注') ax.set_xticks([2,4,6,8,10])#x轴刻度ax.set_yticks([1,3,6,9,12,15,18,20])#y轴...
figure(): 描述:创建一个新的图形窗口。 示例: python import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot() ax.plot([1, 2, 3], [1, 4, 9]) plt.show() 这些函数为matplotlib.pyplot模块提供了强大的绘图能力,支持生成各种类型的图表以满足不同的数据可视化需求。
ax1.set_title('Subplot 1') # 添加第二个子图 ax2 = fig.add_subplot(2, 1, 2) ax2.plot([1, 2, 3], [7, 8, 9]) ax2.set_title('Subplot 2') # 合并两个子图的坐标轴 ax2.set_visible(False) # 显示图形 plt.show() 在上面的示例代码中,首先创建了一个包含两个子图的图形。然后,...
plt.title('Easy as 1, 2, 3') # subplot 211 title 除此之外,类似的有: pyplot.subplot() pyplot.subplots() Figure.subplots() Figure.add_axes() Figure.add_subplot() 6.Annotating text 6.1 Thetext()command can be used to add text in an arbitrary location. ...