How can I change the x axis in matplotlib so there is no white space?

前端 未结 1 756
滥情空心
滥情空心 2020-11-22 14:28

So currently learning how to import data and work with it in matplotlib and I am having trouble even tho I have the exact code from the book.

This is what t

1条回答
  •  清酒与你
    2020-11-22 15:12

    In matplotlib 2.x there is an automatic margin set at the edges, which ensures the data to be nicely fitting within the axis spines. In this case such a margin is probably desired on the y axis. By default it is set to 0.05 in units of axis span. To set the margin to 0 on the x axis, use

    plt.margins(x=0)
    

    or

    ax.margins(x=0)
    

    depending on the context. Also see the documentation.

    In case you want to get rid of the margin in the whole script, you can use

    plt.rcParams['axes.xmargin'] = 0
    

    at the beginning of your script (same for y of course). If you want to get rid of the margin entirely and forever, you might want to change the according line in the matplotlib rc file:

    axes.xmargin : 0
    axes.ymargin : 0
    


    Alternatively to changing the margins, use plt.xlim(..) or ax.set_xlim(..) to manually set the limits of the axes such that there is no white space left.

    0 讨论(0)
提交回复
热议问题