我的子图上的 Y 限制imshow看似任意范围。
在此示例中,我尝试显示 N 次试验的平均值,然后将所有 N 次试验随时间的推移绘制为二维图。
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")
Run Code Online (Sandbox Code Playgroud)
有什么技巧可以正确设置 Y 限制吗?
默认情况下,imshow 使用相等的宽高比。由于 x 轴固定为上图 (ax1) 的范围(即 ),因此1y 轴只能延伸到 的一小部分1。
解决方案实际上很简单:您只需添加
ax2.set_aspect('auto')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3976 次 |
| 最近记录: |