10

I am trying to do a matplolib figure with some padding between the axis and the actual plot.

Here is my example code :

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1)

x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

plt.plot(x, y, 'r')
plt.grid(True)


plt.show()

And here is what I am trying to get :

plot with x and y shift

2 Answers 2

20

In your case, it's easiest to use ax.margins(some_percentage) or equivalently plt.margins(some_percentage).

For example:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

ax.plot(x, y, 'r')
ax.grid(True)
ax.margins(0.05) # 5% padding in all directions

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Note the margins setting applies to autoscale; if this has been turned off, it needs to be re-enabled for margins to have an effect: ax.autoscale(True)
0

you can set the limits of the plot with xlim and ylim. See here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.