matplotlib绘图基础(1)—— subplot

作者: WooWoods | 来源:发表于2019-09-26 23:20 被阅读0次

    当年学matplotlib半途而废了。。。
    现如今决定重新拾起来,在此立个flag,每日记录一个主题,一方面督促自己坚持下去,另一方面方便将来用的时候查阅。

    Subplot

    1. subplot 方法

    np.random.seed(19680801)
    
    dt = 0.01
    t = np.arange(0, 10, dt)
    nse = np.random.randn(len(t))
    r = np.exp(-t / 0.05)
    
    cnse = np.convolve(nse, r) * dt
    cnse = cnse[:len(t)]
    s = 0.1 * np.sin(2 * np.pi * t) + cnse
    
    fig, ax_lst = plt.subplots(2,1)
    # plt.subplot(211)
    ax_lst[0].plot(t, s)
    # plt.subplot(212)
    ax_lst[1].psd(s, 512, 1 / dt)
    
    plt.show()
    
    image.png

    调整子图间距

    fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
    for i in range(2):
        for j in range(2):
            axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)
    plt.subplots_adjust(wspace=0, hspace=0)
    
    image.png

    2. GridSpec 方法

    a flexible way to layout subplot grids. Specifies the geometry of the grid that a subplot can be placed in

    def format_axes(fig):
        for i, ax in enumerate(fig.axes):
            ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
            ax.tick_params(labelbottom=False, labelleft=False)
    
    fig = plt.figure(constrained_layout=True)
    
    gs = GridSpec(3, 3, figure=fig)
    ax1 = fig.add_subplot(gs[0, :])
    # identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3))
    ax2 = fig.add_subplot(gs[1, :-1])
    ax3 = fig.add_subplot(gs[1:, -1])
    ax4 = fig.add_subplot(gs[-1, 0])
    ax5 = fig.add_subplot(gs[-1, -2])
    
    fig.suptitle("GridSpec")
    format_axes(fig)
    
    plt.show()
    
    image.png

    Nested GridSpec

    def format_axes(fig):
        for i, ax in enumerate(fig.axes):
            ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
            ax.tick_params(labelbottom=False, labelleft=False)
    
    
    # gridspec inside gridspec
    f = plt.figure()
    
    gs0 = gridspec.GridSpec(1, 2, figure=f)
    
    gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
    
    ax1 = f.add_subplot(gs00[:2, :])
    ax2 = f.add_subplot(gs00[2, :2])
    ax3 = f.add_subplot(gs00[2, 2])
    
    # the following syntax does the same as the GridSpecFromSubplotSpec call above:
    gs01 = gs0[1].subgridspec(3, 3)
    
    ax4 = f.add_subplot(gs01[:, :-1])
    ax5 = f.add_subplot(gs01[:-1, -1])
    ax6 = f.add_subplot(gs01[-1, -1])
    
    plt.suptitle("GridSpec Inside GridSpec")
    format_axes(f)
    
    plt.show()
    
    image.png

    3. 自定义方法

    left, width = 0.1, 0.65
    bottom, height = 0.1, 0.65
    spacing = 0.005
    
    
    rect_scatter = [left, bottom, width, height]
    rect_histx = [left, bottom + height + spacing, width, 0.2]
    rect_histy = [left + width + spacing, bottom, 0.2, height]
    
    # start with a rectangular Figure
    fig = plt.figure(figsize=(8, 8))
    
    ax_scatter = fig.add_axes(rect_scatter)
    ax_scatter.tick_params(direction='in', top=True, right=True)
    ax_histx = fig.add_axes(rect_histx)
    ax_histx.tick_params(direction='in', labelbottom=False)
    ax_histy = fig.add_axes(rect_histy)
    ax_histy.tick_params(direction='in', labelleft=False)
    
    image.png

    很多看起来比较复杂高级的图形都是通过几种基本图形组合而来的,掌握了以上这几种方法,基本可以满足日常作图的需求了,根据实际需要自由安排。

    相关文章

      网友评论

        本文标题:matplotlib绘图基础(1)—— subplot

        本文链接:https://www.haomeiwen.com/subject/siinuctx.html