Mainland
Mainland

Reputation: 4564

matplotlib zoomed plot window inside a main plot?

I am trying to plot a zoom plot inside the main plot. I was successful in running the code and getting the plots. But there was no zoom-in.

My code:

# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:10]/0.05)               # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt  # colored noise

# the main axes is subplot(111) by default
plt.plot(t, s)

# this is another inset axes over the main axes
a = plt.axes([0.2, 0.6, .2, .2])
plt.plot(t[:len(r)], r)
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])
plt.show()

Present output: enter image description here

Upvotes: 1

Views: 1143

Answers (1)

Alex
Alex

Reputation: 7045

So this code:

dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:10]/0.05)
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt

fig, ax = plt.subplots()
ax.plot(t, s)

inset_ax = ax.inset_axes([0.2, 0.6, 0.2, 0.2])
inset_ax.plot(t[:len(r)], r)

Produces:
Inset plot

Crucially, the min/max of t[:len(r)] is 0/0.009 respectively. So it seems the xlim that you apply is what is making it seem like there is no "zoom"

Upvotes: 3

Related Questions