matplotlib绘制多个子图

from pylab import *
import matplotlib.gridspec as gridspec

"""
subplot()函数的参数中,除最后一维的其它维表示子图的大小,最后一维表示当前子图在图像中的位置,如:在2*2的网格里,第四个子图为(2,2,4)
创建横跨多个位置的子图用gridspec实现
参数	            默认值	                描述
num	            1	                    图像的数量
figsize         figure.figsize          图像的长和宽(英寸)
dpi             figure.dpi              分辨率(点/英寸)
facecolor       figure.facecolor        绘图区域的背景颜色
edgecolor       figure.edgecolor        绘图区域边缘的颜色
frameon         True                    是否绘制图像边缘
"""


def four_sub_graph():
    # 绘制上下左右4个子图
    """
    # 方式一
    fig = plt.figure(figsize=(10,10),dpi=80,facecolor='red')
    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.plot()
    ax2.plot()
    ax3.plot()
    ax4.plot()
    show()
    """
    # 方式二
    for i in range(1, 5):
        subplot(2, 2, i)
        xticks([]), yticks([])
        text(0.5, 0.5, f'subplot(2,2,{i})', ha='center', va='center', size=20, alpha=0.5)
    show()


def mix_sub_graph():
    """
    绘制多个子图,其中某些子图横跨多个子图
    :return:
    """
    # gridspec的用法,可以是图像横跨多个坐标
    G = gridspec.GridSpec(3, 3)

    axes_1 = subplot(G[0, :])
    xticks([]), yticks([])
    text(0.5, 0.5, 'Axes 1', ha='center', va='center', size=24, alpha=.5)

    axex_2 = subplot(G[1, :-1])
    xticks([]), yticks([])
    text(0.5, 0.5, 'Axes 2', ha='center', va='center', size=24, alpha=.5)

    axex_3 = subplot(G[1:, -1])
    # 确定了这个子图的位置之后,就可以直接在上面画图,直接创建了下个新的子图
    x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
    c, s = np.cos(x), np.sin(x)
    plt.plot(x, c)
    plt.plot(x, s)

    # xticks([]), yticks([])
    # text(0.5, 0.5, 'Axes 3', ha='center', va='center', size=24, alpha=.5)

    axex_4 = subplot(G[-1, 0])
    xticks([]), yticks([])

    '''
    text()函数用于在图像上的绑定位置加上一些文本,用于注释
    '''
    text(0.5, 0.5, 'Axes 4', ha='center', va='center', size=24, alpha=.5)
    axes_5 = subplot(G[-1, -2])
    xticks([]), yticks([])
    text(0.5, 0.5, 'Axes 5', ha='center', va='center', size=24, alpha=.5)
    # plt.savefig('../figure/gridspec.png', dpi=64)
    show()

four_sub_graph()
mix_sub_graph()

函数four_sub_graph():
在这里插入图片描述
函数mix_sub_graph():
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值