美文网首页
Matplotlib中figure,axes(subplot),

Matplotlib中figure,axes(subplot),

作者: joy1987 | 来源:发表于2020-04-26 18:16 被阅读0次

    概念

    • figure

    figure是图像的载体,使用pyplot.figure()创建, 一个程序可以创建多个画布, 画图操作作用于最近创建的画布上, 多个画布顺序显示.

    x = np.linspace(-3,3,100)
    y1 = 2*x + 1
    y2 = x**2
    ##第一个画布
    plt.figure()
    plt.plot(x,y1)
    ## 第二个画布
    plt.figure()
    plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
    plt.plot(x,y2)
    plt.show()
    
    • axes (subplot)

    坐标系,可理解为画布上的一个区域, 一个figure上可以有多个axes,被分成多个区域, 可用pyplot.axes和pyplot.subplots/pyplot.subplot来创建. pyplot画图操作本质就是操作axes对象, 他也是就近原则参考代码二

    下面两个代码块的作用一样.

    fig, axess = plt.subplots(2,2)
    ax1 = axess[0,0]
    ax2 = axess[0,1]
    ax3 = axess[1,0]
    ax4 = axess[1,1]
    ax2.plot(np.arange(4),np.arange(4))
    plt.scatter(np.arange(4), np.arange(4))
    plt.show()
    
    plt.subplot(2,2,1) 
    plt.subplot(2,2,2) 
    plt.plot(np.arange(4),np.arange(4))
    plt.subplot(2,2,3)
    plt.subplot(2,2,4)
    plt.scatter(np.arange(4), np.arange(4))
    plt.show()
    
    • axis

    坐标轴,画的图都是基于坐标轴的

    image.png

    相关文章

      网友评论

          本文标题:Matplotlib中figure,axes(subplot),

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