python - 将 imshow 与共享 x 轴与 matplotlib 一起使用

标签 python matplotlib

我的 imshow 子图的 Y 限制停留在看似任意的范围内。

在此示例中,我尝试显示 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")

rogue plot

有什么技巧可以正确设置 Y 限制吗?

最佳答案

默认情况下,imshow 使用相等的纵横比。 由于您的 x 轴固定在上图 (ax1) 的范围内,即 1,因此 y 轴的范围只能是 1 的一小部分。

解决方案其实很简单:你只需要添加

ax2.set_aspect('auto')

关于python - 将 imshow 与共享 x 轴与 matplotlib 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40640333/

相关文章:

python - 在 matplotlib 中绘制多边形的并集

python - matplotlib/pandas条形图的简单定制(标签、刻度等)

python - 使用 Seaborn FacetGrid 绘制时间序列

Java 字符串到 Python 字符串编码 "error"

python - 从字符串末尾分割字符串中的 3 个空格分隔的值?

python - 使用 Pandas 将 SQL 导出到 Excel,如何下拉?

python - 当我尝试启动粘贴服务时 pkg_resources.VersionConflict

python optimize.leastsq : fitting a circle to 3d set of points

python - 制作networkx图,其中边缘仅显示编辑的数值,而不显示字段名称

python - 小方 block 而不是传说中的线条?