Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add spacing to the side of plots in Matplotlib

I am making a lot of plots in Matplotlib where the outer datapoints lie on the edge of the plot space, which obscures often important data. For instance, I would like to do something like:

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

As written, I end up with a plot where the xaxis goes from 0 to 1 and it's really hard to get an impression of what the data look like (plus it wastes a lot of space).

I would like some way to reset the x axis limits so that a) it adds space on each side and b) the xticks cover the whole xaxis at reasonable (default) intervals. Obviously in the single case I could use ax.set_xlim() and ax.set_xticks(), but I am generating a lot of these plots and don't know ahead of time what the optimal way to adjust the plots will be.

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

like image 732
mwaskom Avatar asked Mar 06 '13 01:03

mwaskom


1 Answers

use Axe.margins() to set the margin of xais & yaxis.

ax.margins(0.1, 0.1)

(doc)

like image 191
HYRY Avatar answered Oct 20 '22 18:10

HYRY