R-shaped data data buffer in matplotlib

Counts

R automatically sets the x and y limits to put some space between the data and the axes. I was wondering if there is a way for matplotlib to do the same automatically. If not, is there a good formula or β€œrule of thumb” on how R sets its axis?

+7
python matplotlib r plot
Nov 21
source share
2 answers

Considering ?par for the xaxs parameter:

The style for calculating the spacing of the axis to be used for the x axis. Possible values: "r", "i", "e", "s", "d". Styles are typically controlled by a data range or xlim if specified.
The β€œr” (regular) style first extends the data range by 4 percent at each end, and then finds an axis with beautiful labels that fits in the extended range.

So, if we try this:

 par(xaxs="r") #The default plot(1:10, 1:10) 

enter image description here

We can actually demonstrate that it is +/- 4% of the range on each side:

 abline(v=1-(diff(range(1:10)))*0.04, col="red") abline(v=10+(diff(range(1:10)))*0.04, col="red") 

enter image description here

+6
Nov 21 '12 at 3:08
source share

In matplotlib you can achieve this by setting margins

 import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.margins(0.04) data = range(1, 11) ax.plot(data, 'wo') plt.savefig('margins.png') 

margins

However, it seems that there is an rc parameter to get this parameter.

Update 4/2013

The ability to add rc param for fields is now in matplotlib master (Thanks @tcaswell). Therefore, it should work with the next version of matplotlib (the current version is 1.2.1).

+7
Nov 21 '12 at 6:57
source share



All Articles