Set autoscale limits on plot to have buffer around all points

前端 未结 2 1079
轻奢々
轻奢々 2020-12-31 13:58

I would like to plot a set of points using pyplot in matplotlib but have none of the points be on the edge of my axes. The autoscale (or something) sets the xlim

相关标签:
2条回答
  • 2020-12-31 14:14

    A similar question was posed to the matplotlib-users list earlier this year. The most promising solution involves implementing a Locator (based on MaxNLocator in this case) to override MaxNLocator.view_limits.

    0 讨论(0)
  • 2020-12-31 14:23

    @askewchan I just succesfully achieved how to change matplotlib settings by editing matplotlibrc configuration file and running python directly from terminal. Don't know the reason yet, but matplotlibrc is not working when I run python from spyder3 (my IDE). Just follow steps here matplotlib.org/users/customizing.html.

    1) Solution one (default for all plots)

    Try put this in matplotlibrc and you will see the buffer increase:

    axes.xmargin        : 0.1  # x margin.  See `axes.Axes.margins`
    axes.ymargin        : 0.1  # y margin See `axes.Axes.margins`
    

    Values must be between 0 and 1.

    Obs.: Due to bugs, scale is not correctly working yet. It'll be fixed for matplotlib 1.5 (mine is 1.4.3 yet...). More info:

    • axes.xmargin/ymargin rcParam behaves differently than pyplot.margins() #2298
    • Better auto-selection of axis limits #4891

    2) Solution two (individually for each plot inside the code)

    There is also the margins function (for put directly in the code). Example:

    import numpy as np
    from matplotlib import pyplot as plt 
    t = np.linspace(-6,6,1000)
    plt.plot(t,np.sin(t))
    plt.margins(x=0.1, y=0.1)
    plt.savefig('plot.png')
    

    Obs.: Here scale is working (0.1 will increase 10% of buffer before and after x-range and y-range).

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