gpt4 book ai didi

python - 绘图 : imshow 3D array with a slider

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:47 24 4
gpt4 key购买 nike

我有一个 3d 数组

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

A = np.random.rand(10,5,5)

我想在 A

中可视化每个 5x5 图像
for Ai in A:
plt.imshow(Ai)
plt.show()

除了上面示例中的 5 个数字之外,我希望有一个 slider 来在 A 的第一个坐标的索引之间切换。

目前,我尝试了以下方法:

idx0 = 3
l = plt.imshow(A[idx0])

axidx = plt.axes([0.25, 0.15, 0.65, 0.03])
slidx = Slider(axidx, 'index', 0, 9, valinit=idx0, valfmt='%d')

def update(val):
idx = slidx.val
l.set_data(A[idx])
fig.canvas.draw_idle()
slidx.on_changed(update)

plt.show()

但是当我使用 slider 时,图中的图像没有改变,我得到了消息

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

如何让我的 slider 与我的 3d 阵列一起工作?

最佳答案

事实证明,问题根本不在于 slider ,我只需要将 slider 返回的值转换为 int

替换为

l.set_data(A[int(idx)])

成功了

关于python - 绘图 : imshow 3D array with a slider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55401246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com