7

The Y limits on my imshow subplot are stuck on a seemingly arbitrary range.

In this example, I'm trying to show the mean of N trials and then plot all the N trials over time as a 2d plot.

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
N = 20   # number of trials
M = 3000  # number of samples in each trial
data = np.random.randn(N, M)
x = np.linspace(0,1,M)  # the M samples occur in the range 0-1 
                    # ie the sampling rate is 3000 samples per second
f, (ax1, ax2) = plt.subplots(2,1, sharex=True)
ax1.plot(x, np.mean(data, 0))
ax2.imshow(data, cmap="inferno", interpolation="nearest", extent=[0, 1, 0, N])
ax2.set_ylim(0, N)
ax1.set_ylabel("mean over trials")
ax2.set_ylabel("trial")
ax2.set_xlabel("time")

rogue plot

Are there any tricks to set the Y limits correctly?

3
  • Could you specify what you are trying to achieve with the second plot (e.g. detail the use of extent, why you plot data and not data.T, etc.)? Commented Nov 16, 2016 at 19:22
  • @P.Camilleri Added some motivating text to question. Commented Nov 16, 2016 at 19:30
  • @P.Camilleri added labels to plot as well. Commented Nov 16, 2016 at 19:48

1 Answer 1

9

By default, imshow uses an equal aspect ratio. Since your x-axis is fixed to the extent of the plot above (ax1), which is 1, the y-axis can only extent to a fraction of 1.

The solution is actually quite simple: You just need to add

ax2.set_aspect('auto')
Sign up to request clarification or add additional context in comments.

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.