Matplotlib: set axis tight only to x or y axis

后端 未结 2 874
醉话见心
醉话见心 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:58

    I just put the following at the beginning of those scripts in which I know I'll want my xlims to hug my data:

    import matplotlib.pyplot as plt
    plt.rcParams['axes.xmargin'] = 0
    

    If I decide to add some whitespace buffer to an individual plot in that same script, I do it manually with:

    plt.xlim(lower_limit, upper_limit)
    

    While the accepted answer works, and is what I used for a while, I switched to this strategy because I find it a lot easier to remember.

提交回复
热议问题