Pyplot:imshow三维数组

2024-10-04 05:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个3d阵列

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

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

我希望在A中可视化每个5x5图像

^{pr2}$

除了上面例子中的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

如何让我的滑块与我的三维阵列工作?在


Tags: 图像importnumpymatplotlibasnpupdateplt