如何刷新Matplotlib图的轴图像

CL。

我想使用Matplotlib逐片绘制3D体积。鼠标滚动以更改索引。我的程序如下:

#Mouse scroll event.
def mouse_scroll(event):
fig = event.canvas.figure
  ax = fig.axes[0]
  if event.button == 'down':
    next_slice(ax)
  fig.canvas.draw()

#Next slice func.
def previous_slice(ax):
  volume = ax.volume
  ax.index = (ax.index - 1) % volume.shape[0]
  #ax.imshow(volume[ax.index])
  ax.images[0].set_array(volume[ax.index])

图在主函数中初始化。喜欢:

 fig, ax = plt.subplots()
 ax.volume = volume # volume is a 3D data, a 3d np array.
 ax.index = 1
 ax.imshow(volume[ax.index])
 fig.canvas.mpl_connect('scroll_event', mouse_scroll)

即使我不明白什么是一切,一切都运转良好ax.images但是,当我用ax.volume新的卷数据替换时发生了问题它突然停止渲染!调试代码,ax.image[0]在每个事件回调中正确设置。

但是,如果将image set_array方法更改为ax.show()图再次开始渲染。但是与该ax.images[0].set_array()方法相比,轴显示功能确实很慢

我该如何解决这个问题?真的很想用set_array()方法。非常感谢你。

附加了一个简单的可执行脚本。plot.py@googledrive

认真的重要性

您需要一直在同一张图片上工作。最好给这个起个名字

img = ax.imshow(volume[ax.index])

然后,您可以使用设置数据set_data

import numpy as np
import matplotlib.pyplot as plt

#Mouse scroll event.
def mouse_scroll(event):
    fig = event.canvas.figure
    ax = fig.axes[0]
    if event.button == 'down':
        next_slice(ax)
    fig.canvas.draw()

#Next slice func.
def next_slice(ax):
    volume = ax.volume
    ax.index = (ax.index - 1) % volume.shape[0]
    img.set_array(volume[ax.index])

def mouse_click(event):
    fig = event.canvas.figure
    ax = fig.axes[0]
    volume = np.random.rand(10, 10, 10)
    ax.volume = volume
    ax.index = (ax.index - 1) % volume.shape[0]              
    img.set_array(volume[ax.index])
    fig.canvas.draw_idle()

if __name__ == '__main__':
    fig, ax = plt.subplots()
    volume = np.random.rand(40, 40, 40)
    ax.volume = volume # volume is a 3D data, a 3d np array.
    ax.index = 1
    img = ax.imshow(volume[ax.index])
    fig.canvas.mpl_connect('scroll_event', mouse_scroll)
    fig.canvas.mpl_connect('button_press_event', mouse_click)
    plt.show()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章