切片数组中 matshow 子图的刻度

问题描述 投票:0回答:1

我的 numpy 数组的形状为 (500,6)。因为它非常高,所以我用 matplotlib matshow 绘制它的切片。

这是我的代码:

fig, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(1, 5, figsize=(6,12), layout='constrained', sharex=True)

min = q_func.min()
max = q_func.max()

plt.rcParams.update({"text.usetex": True,"font.family": "Computer Modern Serif"})
ax1.matshow(q_func[0:99,:], vmin=min, vmax=max, aspect='equal', cmap='viridis')
ax2.matshow(q_func[100:199,:], vmin=min, vmax=max, aspect='equal', cmap='viridis')
ax3.matshow(q_func[200:299,:], vmin=min, vmax=max, aspect='equal', cmap='viridis')
ax4.matshow(q_func[300:399,:], vmin=min, vmax=max, aspect='equal', cmap='viridis')
im = ax5.matshow(q_func[400:499,:], vmin=min, vmax=max, aspect='equal', cmap='viridis')

fig.supxlabel("Acciones")
fig.supylabel("Estados")
fig.suptitle("$Q$-función aproximada con 2000 episodios")
fig.colorbar(im)

哪个输出:

output image

但我希望第二个子图 y 刻度与切片数组的刻度相匹配,因此它们从原点开始为 100, 120, ... 其他子数字相同:200、220、...; 300、320、...; 400、420、... 所以它应该看起来像这样:

Desired output

但到目前为止我还没有运气。如果有人知道如何实现这一目标,我将不胜感激。

我尝试过 ax.set_yticks、ax.set_yticklabels。但到目前为止我还没有运气。如果有人知道如何实现这一目标,我将不胜感激。


编辑: 我尝试添加

ax2.set_yticks(ax2.get_yticks())
ax2.set_yticklabels(["","",100,120,140,160,180])

这使得这个

ax2.set_yticks([i for i in range(100,200)])

给出这个

python matplotlib
1个回答
0
投票

您可以选择当前刻度并用刻度 + 100 对其进行标记。

ax2.set_yticks(range(0, 100, 20), labels=range(100, 200, 20))

您可以通过循环编写来避免代码重复。

for i, ax in enumerate([ax1, ax2, ax3, ax4]):
    ax.set_yticks(range(0, 100, 20), labels=range(i*100, i*100+100, 20))
© www.soinside.com 2019 - 2024. All rights reserved.