Matplotlib: set axis tight only to x or y axis

后端 未结 2 878
醉话见心
醉话见心 2020-12-05 17:54

I have a plot look like this:

Obviously, the left and right side is a waste of space, so I set

plt.axis(\'tight\')

But thi

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 18:48

    You want to use matplotlib's autoscale method from the matplotlib.axes.Axes class.

    Using the functional API, you apply a tight x axis using

    plt.autoscale(enable=True, axis='x', tight=True)
    

    or if you are using the object oriented API you would use

    ax = plt.gca()  # only to illustrate what `ax` is
    ax.autoscale(enable=True, axis='x', tight=True)
    

    For completeness, the axis kwarg can take 'x', 'y', or 'both', where the default is 'both'.

提交回复
热议问题