我正在尝试将 Mayavi 与 Jupyter Notebook 中的工作流程集成。以下精简的代码示例可能看起来不必要的复杂,但这些是我不允许更改的限制。
该类Test
用于将某些东西绘制成mlab
图形。该类Wrapper
实例化Test
该类,而该wrapper_func()
函数向用户公开:它返回Wrapper
实例并应mlab
在关键字参数时显示图形show=True
。
注意:我不允许更改Wrapper
和wrapper_func
. 此外,Test
该类应实现该show()
方法。
from mayavi import mlab
mlab.init_notebook()
import numpy as np
class Test:
def __init__(self):
self.fig = mlab.figure()
self._add_data()
def _add_data(self):
pi = np.pi
cos = np.cos
sin = np.sin
dphi, dtheta = pi / 250.0, pi / 250.0
[phi, theta] = np.mgrid[0:pi + dphi * 1.5:dphi,
0:2 * pi + dtheta * 1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3
m4 = 6; m5 = 2; m6 = 6; m7 = 4
r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + \
sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7
x = r * sin(phi) * cos(theta)
y = r * cos(phi)
z = r * sin(phi) * sin(theta)
mlab.mesh(x, y, z, figure=self.fig)
def show(self):
return self.fig
class Wrapper:
def __init__(self):
self.t = Test()
def show(self):
self.t.show()
def wrapper_func(show=True):
w = Wrapper()
if show:
w.show()
return w
然后,用户在 Jupyter Notebook 单元格中写入:
wrapper_func()
这应该在输出单元格中显示绘图。不幸的是,该图未显示。
或者,用户可以执行以下操作:
w = wrapper_func(show=False)
w.show()
我该怎么做才能在输出单元格中显示图形?