Programmatically add distance to plots in Matplotlib

I do a lot of graphing in Matplotlib, where external data points lie on the edge of the plot space, which often hides important data. For example, I would like to do something like:

f, ax = subplots(1, 1) ax.plot([0, 1], [.25, .5], "o") 

As written, I’m ending up with a plot where xaxis goes from 0 to 1, and it’s really difficult to get an idea of ​​how the data looks (plus it spends a lot of space).

I would like to somehow limit the reset of the limits of the x axis so that a) it adds space on each side and b) xticks span the entire xaxis with reasonable (default) intervals. Obviously, in one case, I could use ax.set_xlim () and ax.set_xticks (), but I generate a lot of these graphs and I don’t know in advance which optimal way to adjust the graphs would be.

Is there any option in Matplotlib to set the percentage of the x data range that spans the x axis or similar?

+6
source share
1 answer

use Axe.margins() to set the xais and yaxis fields.

 ax.margins(0.1, 0.1) 

(doc)

+6
source

All Articles