我有一个3d阵列
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
A = np.random.rand(10,5,5)
我希望在A
中可视化每个5x5图像
除了上面例子中的5个数字之外,我希望有一个滑块来在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()
但是当我使用滑块时,图中的图像没有改变,我得到了信息
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
如何让我的滑块与我的三维阵列工作?在
结果,问题根本不在于滑块,我只需要将滑块返回的值转换为
int
。在替换为
有办法吗
相关问题 更多 >
编程相关推荐