Matplotlib margins/padding when using limits

Viewed 266

I'm trying to set xlimits and keep the margins. In a simplified code, the dataset contains 50 values. When plotting the whole data set, it is fine. However, I only want to plot values 20-40. The plot starts and ends without having any margins.

How do I plot values 20-40 but keep the margins? Online I found to ways to play with the margin/padding

1) plt.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)

2) ax1.margins(0.05)

Both, however, do not seem to work when using xlimits.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1, 200, 50)
y = np.random.random(len(x))

fig_1 = plt.figure(figsize=(8, 4)) 
ax1 = plt.subplot(1,1,1)

ax1.plot(x, y)
ax1.set_xlim(x[19], x[40])  

# ax1.plot(x[19:40], y[19:40]) 
# would create exactly the plot I want. But it is not the solution I am looking for. 
# I cannot change/slice the data. I want to change the figure.

Image

0 Answers
Related