Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add margin when plots run against the edge of the graph

Often when I plot in matplotlib, I get graphs like this:

frequency response running along top edge

You can't see the function because it runs against the edge of the plot.

Is there any way to automatically add some margin in these cases, so that they look like this:

frequency response with tiny amount of noise

like image 704
endolith Avatar asked Jan 24 '13 03:01

endolith


People also ask

How do you set plot margins in R?

To visualize how R creates plot margins, look at margin Figure 11.20. You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).

How do you find the default margin of a graph using par function?

par(mai=c(1.02,0.82,0.82,0.42)) The numbers used above are the default margin settings in R. You can verify this by firing up the R prompt and typing par(“mar”) or par(“mai”). You should get back a vector with the above values.

What is OMA R?

oma() for outer margin area.


1 Answers

You can use ax.margins() to set the margins. Example:

In [1]: fig, ax = plt.subplots()

In [2]: ax.plot(np.arange(10), '-o')
Out[2]: [<matplotlib.lines.Line2D at 0x302fb50>]

without margin

In [1]: fig, ax = plt.subplots()

In [2]: ax.margins(0.05)

In [3]: ax.plot(np.arange(10), '-o')
Out[3]: [<matplotlib.lines.Line2D at 0x302fb50>]

with margin

You can also set only the x- or the y-margin. However it doesn't seem to be a matplotlibrc option so that you can simply make this the default behaviour (so it isn't fully automatically). I opened a github issue to request this.

like image 122
bmu Avatar answered Oct 13 '22 20:10

bmu