如何刷新 Matplotlib 图形轴的图像
How to refresh images of axes of Matplotlib figure
我想使用 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]
。
但是,如果将图像 set_array 方法更改为 ax.show()
。人形再次开始渲染。但是与 ax.images[0].set_array()
方法相比,axes imshow 函数真的很慢。
我该如何解决这个问题?真想用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()
我想使用 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]
。
但是,如果将图像 set_array 方法更改为 ax.show()
。人形再次开始渲染。但是与 ax.images[0].set_array()
方法相比,axes imshow 函数真的很慢。
我该如何解决这个问题?真想用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()