Often when I plot in matplotlib, I get graphs like this:
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:
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).
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.
oma() for outer margin area.
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>]
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>]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With