Matplotlib lining between section and axis

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

Here is my sample 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:

graph with shift x and y

+4
source share
2 answers

In your case, it is easiest to use ax.margins(some_percentage)or equivalent 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

+8
source

you can set graph limits using xlim and ylim. See here

+1
source

Source: https://habr.com/ru/post/1609770/


All Articles