Python绘图布局、图例说明、边界

    技术2022-07-11  161

    当绘画完成后,会发现XY轴的区间是会自动调整的,并不是跟传入的XY轴数据中的最值相同。为了调整区间可以使用下面的方式:

    ax.set_xlim([xmin, xmax])   #设置X轴的区间

    ax.set_ylim([ymin, ymax])   #Y轴区间

    ax.axis([xmin, xmax, ymin, ymax])   #XY轴区间

    ax.set_ylim(bottom=-10)     #Y轴下限

    ax.set_xlim(right=25)       #X轴上限

    具体效果如下:

    x = np.linspace(0, 2*np.pi)

    y = np.sin(x)

    fig, (ax1, ax2) = plt.subplots(2)

    ax1.plot(x, y)

    ax2.plot(x, y)

    ax2.set_xlim([-1, 6])

    ax2.set_ylim([-1, 3])

    plt.show()

    可以看出修改了区间之后影响了图片显示的效果。

    2 图例说明

    如果在一个Axes上做多次绘画,那么可能出现分不清哪条线或点所代表的意思。这个时间添加图例说明,就可以解决这个问题了,见下例:

     

    fig, ax = plt.subplots()

    ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='Philadelphia')

    ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='Boston')

    ax.scatter([1, 2, 3, 4], [20, 10, 30, 15], label='Point')

    ax.set(ylabel='Temperature (deg C)', xlabel='Time', title='A tale of two cities')

    ax.legend()

    plt.show()

    在绘图时传入 label 参数,并最后调用ax.legend()显示体力说明,对于 legend 还是传入参数,控制图例说明显示的位置:

    Location String

    Location Code

    ‘best’

    0

    ‘upper right’

    1

    ‘upper left’

    2

    ‘lower left’

    3

    ‘lower right’

    4

    ‘right’

    5

    ‘center left’

    6

    ‘center right’

    7

    ‘lower center’

    8

    ‘upper center’

    9

    ‘center’

    10

    3 区间分段

    默认情况下,绘图结束之后,Axes 会自动的控制区间的分段。见下例:

     

    data = [('apples', 2), ('oranges', 3), ('peaches', 1)]

    fruit, value = zip(*data)

     

    fig, (ax1, ax2) = plt.subplots(2)

    x = np.arange(len(fruit))

    ax1.bar(x, value, align='center', color='gray')

    ax2.bar(x, value, align='center', color='gray')

     

    ax2.set(xticks=x, xticklabels=fruit)

     

    ax.tick_params(axis='y', direction='inout', length=10) #修改 ticks 的方向以及长度

    plt.show()

    上面不仅修改了X轴的区间段,并且修改了显示的信息为文本。

    4 布局

    当绘画多个子图时,就会有一些美观的问题存在,例如子图之间的间隔,子图与画板的外边间距以及子图的内边距:

     

    fig, axes = plt.subplots(2, 2, figsize=(9, 9))

    fig.subplots_adjust(wspace=0.5, hspace=0.3,

                        left=0.125, right=0.9,

                        top=0.9,    bottom=0.1)

     

    fig.tight_layout() 自动调整布局,使标题之间不重叠

    plt.show()

    通过fig.subplots_adjust()修改了子图水平之间的间隔wspace=0.5,垂直方向上的间距hspace=0.3,左边距left=0.125 等等,这里数值都是百分比的。以 [0, 1] 为区间,选择leftrightbottomtop 注意 top right 0.9 表示上、右边距为百分之10。不确定如果调整的时候,fig.tight_layout()是一个很好的选择。之前说到了内边距,内边距是子图的,也就是 Axes 对象,所以这样使用 ax.margins(x=0.1, y=0.1),当值传入一个值时,表示同时修改水平和垂直方向的内边距。

     

    观察上面的四个子图,可以发现他们的XY的区间是一致的,而且这样显示并不美观,所以可以调整使他们使用一样的XY轴:

     

    fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)

    ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])

    ax2.plot([3, 4, 5, 6], [6, 5, 4, 3])

    plt.show()

     

    5 轴相关

    改变边界的位置,去掉四周的边框:

     

    fig, ax = plt.subplots()

    ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5])

    ax.spines['top'].set_visible(False)     #顶边界不可见

    ax.xaxis.set_ticks_position('bottom')   #ticks 的位置为下方,分上下的。

    ax.spines['right'].set_visible(False)   #右边界不可见

    ax.yaxis.set_ticks_position('left') 

     

    #"outward"

    #移动左、下边界离 Axes 10 个距离

    #ax.spines['bottom'].set_position(('outward', 10))

    #ax.spines['left'].set_position(('outward', 10))

     

    #"data"

    #移动左、下边界到 (0, 0) 处相交

    ax.spines['bottom'].set_position(('data', 0))

    ax.spines['left'].set_position(('data', 0))

     

    #"axes"

    #移动边界,按 Axes 的百分比位置

    #ax.spines['bottom'].set_position(('axes', 0.75))

    #ax.spines['left'].set_position(('axes', 0.3))

     

    plt.show()

    作者:QinL 

     

    Processed: 0.011, SQL: 10