matplotlib 操作子图(subplot,axes)

本文介绍了使用Matplotlib进行子图布局的方法,包括subplots、subplot2grid和GridSpec三种方式,展示了如何通过这些方法自定义子图的位置及大小。

Matplotlib 中文用户指南 3.3 使用 GridSpec 自定义子图位置

  • ax:matplotlib.axes._subplots.AxesSubplot,的基本操作
    • ax.set_xticks([]), ax_set_yticks([]):关闭坐标刻度
    • ax.axis('off'):关闭坐标轴
    • ax.set_title():设置标题

1. subplots

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
ax[0].plot(...)
ax[0].set_xlabel(...)
ax[0].set_title(...)

ax[1].plot(...)
ax[1].set_xlabel(...)
...
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)

更一般的做法(也是matlab的风格)是这样的:

fig = plt.figure()          # 创建一个figure对象,底下的一切显示均在此figure上完成

plt.subplot(121)
plt.imshow(img1)
plt.subplot(122)
plt.imshow(img2)

plt.show()

# 当然放在一个range里边
for i in range(ndim):
    plt.subplot(ndim//5, 5, i+1)
    plt.hist()

2. subplot2grid

  • subplot2grid

    
    # 以下两条语句等价
    
    ax = plt.subplot2grid((2,2),(0, 0))
    ax = plt.subplot(221)       # 下标从 0 开始
    • colspan、rowspan 与 索引的对应关系
    import matplotlib.pyplot as plt
    
    def disable_axis(ax):
        ax.set_xticks([])
        ax.set_yticks([])
        # ax.axis('off')
    
    def set_title(ax, title):
        ax.set_title(title)
    
    def subplot2grid_demo():
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)
        disable_axis(ax1)
        set_title(ax1, 'ax1')
        ax2 = plt.subplot2grid((3, 3), (1, 0))
        disable_axis(ax2)
        set_title(ax2, 'ax2')
        ax3 = plt.subplot2grid((3, 3), (1, 1))
        disable_axis(ax3)
        set_title(ax3, 'ax3')
        ax4 = plt.subplot2grid((3, 3), (0, 2), rowspan=2)
        disable_axis(ax4)
        set_title(ax4, 'ax4')
        ax5 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
        disable_axis(ax5)
        set_title(ax5, 'ax5')
        # plt.xticks([])
        # plt.yticks([])
        # plt.axis('off')
        plt.show()
    
    if __name__ == '__main__':
        subplot2grid_demo()



3. GridSpec:方便的切片操作

GridSpec 提供了十分方便的切片操作,实现上述功能,则只需如下代码:

def gridspec_demo():
    gs = gridspec.GridSpec(3, 3)
    ax1 = plt.subplot(gs[0, :2])
    ax2 = plt.subplot(gs[1, 0])
    ax3 = plt.subplot(gs[1, 1])
    ax4 = plt.subplot(gs[0:2, 2])
    ax5 = plt.subplot(gs[2:, :])
    plt.show()
Matplotlib是一个Python的绘库,用于制作各种类型的表,包括线、柱状、散点等。其中,figure、axesMatplotlib中的三个重要概念。 1. figure figure是Matplotlib中最顶层的容器,用于存放所有的绘元素。它可以看作是整个表的画布,可以设置表的大小、分辨率和背景颜色等属性。 2. 是指在同一个figure中划分出来的不同区域,每个区域可以绘制不同的表。可以通过subplot函数来创建,它接受三个参数,分别表示的行数、列数和编号。 例如,以下代码会创建一个2x2的,并分别在每个中绘制一幅表: ``` import matplotlib.pyplot as plt # 创建一个2x2的 fig, axs = plt.subplots(nrows=2, ncols=2) # 在第一个中绘制一幅线 axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3]) # 在第二个中绘制一幅柱状 axs[0, 1].bar(['A', 'B', 'C', 'D'], [10, 5, 20, 15]) # 在第三个中绘制一幅散点 axs[1, 0].scatter([1, 2, 3, 4], [1, 4, 2, 3]) # 在第四个中绘制一幅饼 axs[1, 1].pie([10, 5, 20, 15], labels=['A', 'B', 'C', 'D']) plt.show() ``` 3. axes axes是指中的坐标系,它可以看作是中的一个小画板,可以在上面绘制各种表元素。每个中都有一个默认的axes,可以通过gca函数来获取。 例如,在上面的代码中,可以通过以下代码来获取第一个axes: ``` ax = axs[0, 0].gca() ``` 需要注意的是,axes是可以重叠的,即在同一个中可以添加多个axes。可以通过add_axes函数来添加一个新的axes。 例如,以下代码会在第一个中添加一个新的axes,并在上面绘制一幅散点: ``` ax = axs[0, 0].add_axes([0.1, 0.1, 0.8, 0.8]) ax.scatter([1, 2, 3, 4], [1, 4, 2, 3]) ``` 综上所述,figure、axesMatplotlib中的三个重要概念,它们分别代表整个表的画布、划分出来的不同区域和中的坐标系。每个概念都有其独特的作用和属性,可以根据需要进行灵活运用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值