matplotlib多种子图创建与排版总结(一):subplot(),subplots(),add_subplots,add_axes

注:部分内容参考了:https://blog.csdn.net/helunqu2017/article/details/78662877?

#导入需要的包
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
%matplotlib inline

1.先建画板
语法:figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

  • num:图像编号或名称,数字为编号 ,字符串为名称
  • figsize:指定figure的宽和高,单位为英寸
  • dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
  • facecolor:背景颜色
  • edgecolor:边框颜色
  • frameon:是否显示边框
#1.建画板fig
fig1=plt.figure("我是画板1",figsize=(10,8),dpi=100,facecolor="r",edgecolor="blue",frameon=True)
plt.show()

2."贴"画纸ax,但是要创建子图(即选择“贴”在画板fig的哪个位置,若有多个子图就涉及到各子图的排版问题)
**法1.subplot(nrows,ncols,sharex,sharey,subplot_kw,fig_kw)创建单个子图

  • nrows:subplot的行数;
  • ncols:subplot的列数;
  • sharex:所有subplot应该使用相同的x轴刻度(调节xlim将会影响所有subplot);
  • sharey:所有subplot应该使用相同的y轴刻度(调节ylim将会影响所有subplot);
  • subplot_kw:用于创建个subplot关键字字典;
  • **fig_kw:创建figure时的其他关键字,如:plt.subplots(2,2,figsize=(8,6))
x=np.arange(0,100)#numpy.arange([start, ]stop, [step, ]dtype=None)
#做图1
ax1=plt.subplot(231)
plt.plot(x,x)
#做图2
ax1=plt.subplot(235)
plt.plot(x,-x,color="red",linewidth=5)
#做图3
ax1=plt.subplot(233)
plt.plot(x,x**2)
#做图4
ax1=plt.subplot(232)
plt.plot(x**2,x)
#做图5
ax1=plt.subplot(234)
plt.plot(x**2,-x)
#做图6
ax1=plt.subplot(236)
plt.plot(x**2,1/x)

在这里插入图片描述
matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)
plt.plot()参数参考:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

法2.subplots创建多个子图
subplots()参数与subplot()类似。

fig2,axes=plt.subplots(2,2)
ax1=axes[0,0]#子图编号是从0开始的
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]
#作图1
ax1.plot(x, x)  
#作图2
ax2.plot(x, -x)
#作图3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.8)
#作图4
ax4.plot(x, np.log(x))  
plt.show()

在这里插入图片描述
法3.面向对象API:add_subplots与add_axes新增子图或区域
add_subplot与add_axes都是面对象figure编程的,pyplot api中没有此命令
add_subplot的参数与subplots的相似

import numpy as np  
import matplotlib.pyplot as plt  
x = np.arange(0, 100)  
#新建figure对象
fig=plt.figure()
#新建子图1
ax1=fig.add_subplot(2,2,1)      
ax1.plot(x, x) 
#新建子图3
ax3=fig.add_subplot(2,2,3)
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#新建子图4
ax4=fig.add_subplot(2,2,4)
ax4.plot(x, np.log(x))  
plt.show()
``
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200615204116239.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0tvbmdqaWFuZ2V6aQ==,size_16,color_FFFFFF,t_70)
add_axes为新增子区域,该区域可以座落在figure内任意位置,且该区域可任意设置大小
add_axes参数可参考官方文档:http://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用两种方式来调整图像的子图布局:plt.subplots_adjust和ax.subplots_adjust。 1. plt.subplots_adjust:这是 matplotlib.pyplot 模块中的函数,可以直接在 plt 对象上调用。此函数可以调整整个图像的子图布局,包括所有的子图和图像边框。使用该函数时,需要将所有的子图都使用 plt.subplot() 或 plt.axes() 创建,并将它们保存在一个变量中。然后,可以使用 plt.subplots_adjust() 来调整这些子图的布局。 示例代码如下: ```python import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9, wspace=0.4, hspace=0.4) ``` 2. ax.subplots_adjust:这是 matplotlib.axes.Axes 类的方法,在每个 Axes 对象上调用。它用于调整单个子图的布局,可以更精确地控制每个子图之间的间距和位置。使用该方法时,需要先创建每个子图Axes 对象,并将它们保存在一个变量中。然后,可以使用 ax.subplots_adjust() 来调整每个子图的布局。 示例代码如下: ```python import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) ax1.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9) ax2.subplots_adjust(left=0.2, right=0.8, bottom=0.2, top=0.8) ax3.subplots_adjust(left=0.3, right=0.7, bottom=0.3, top=0.7) ax4.subplots_adjust(left=0.4, right=0.6, bottom=0.4, top=0.6) ``` 总结:如果您需要一次性调整整个图像的子图布局,使用 plt.subplots_adjust();如果您需要分别调整每个子图的布局,使用 ax.subplots_adjust()。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值