subplots(2, 2, sharex=True, sharey=True) # 在第一个子图(ax11)中绘制散点图 ax11.scatter([1, 2], [1, 2]) # 调整子图之间的间距,以防止重叠 plt.tight_layout() # 显示图形 plt.show() def plot_in_plot(): # 初始化figure对象 fig = plt.figure() #...
importmatplotlib.pyplotaspltimportnumpyasnp# 创建2x2的子图布局fig,axs=plt.subplots(2,2,figsize=(10,8))# 生成一些示例数据x=np.linspace(0,10,100)y1=np.sin(x)y2=np.cos(x)y3=np.exp(-x/10)y4=x**2# 在每个子图中绘制不同的函数axs[0,0].plot(x,y1)axs[0,0].set_title('Sine Func...
fig,axs=plt.subplots(2,2,figsize=(12,10))x=np.linspace(0,2*np.pi,100)foriinrange(2):forjinrange(2):axs[i,j].plot(x,np.sin(x+i*np.pi/2+j*np.pi/4))axs[i,j].set_title(f'Sine Wave{i*2+j+1}- how2matplotlib.com')plt.tight_layout()plt.show() Python Copy Output: ...
plt.rcParams['ytick.direction'] = 'in' # 将y轴的刻度方向设置向内 x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] y = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] fig, ax1 = plt.subplots() ax1.plot(x, y, "b--") ax1.plot(x, y[::-...
fig, ax = plt.subplots() box_plot = ax.boxplot((data1, data2, data3, data4, data5), labels=labels, boxprops={'color': 'black'}, showmeans=True, patch_artist=True, ) colors = ['pink', 'blue', 'green', 'yellow', 'red'] ...
= plt.figure() 04 # 生成一个画布fig,fig中有2×2分布均匀的子图 05 fig, axes_list = plt.subplots(2, 2) 06 #构造X轴和Y轴数据07 x = np.linspace(0, 2 * np.pi, 400) 08 y = np.sin(x**2) 09 #在第1行第1列的子图中绘图 10 axes_list[1, 1].plot(x, y) 11 plt.show()...
x = np.linspace(0, 2 * np.pi, 50)offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False)yy = np.transpose([np.sin(x + phi) for phi in offsets])fig, ax = plt.subplots(figsize=(8, 4))ax.set_prop_cycle(line_prop_cycler) # Set propcycle before plottingax.plot(x, yy...
fig,ax=plt.subplots()是对象式编程,这里plt.subplots()是返回一个元组,包含了 figure 对象(控制总体图形大小)和 axes 对象(控制绘图,坐标之类的)。此外fig.add_subplot()也是相同的道理。 进行对象式绘图,首先是要通过plt.subplots()将 figure 类和 axes 类实例化也就是代码中的fig,ax,然后通过 fig 调整整体...
plt.plot(t2, np.cos(2*np.pi*t2),'r--') plt.show() 如果不指定figure()的轴,figure(1)命令默认会被建立,同样的如果你不指定subplot(numrows, numcols, fignum)的轴,subplot(111)也会自动建立。 importmatplotlib.pyplotasplt plt.figure(1)# 创建第一个画板(figure)plt.subplot(211)# 第一个画板...
plt.subplots调用后将会产生一个图表(Figure)和默认网格(Grid),与此同时提供一个合理的控制策略布局子绘图。 一、只有子图的绘制 如果没有提供参数给subplots将会返回: Figure一个Axes对象 例子: fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('A single plot') ...