【发布时间】:2020-05-25 15:23:53
【问题描述】:
我目前正在处理医学图像并编写代码以准备训练集。 为此,我需要滚动浏览体积数据。
我的主要 IDE 是 Spyder,但 IndexTracker 对象的标准实现在函数内对我不起作用。
这个标准实现对我有用: https://matplotlib.org/gallery/animation/image_slices_viewer.html
但是一旦我将绘图的创建放入一个函数中,创建的绘图就不再是可滚动的:
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
class IndexTracker(object):
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = self.slices//2
self.im = ax.imshow(self.X[:, :, self.ind])
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
def plot(X):
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, X)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()
plot(np.random.rand(200, 200, 500))
可能是什么问题?如何从函数中创建可滚动绘图?
【问题讨论】:
标签: python function matplotlib jupyter spyder