在同一图形中结合mayavi和matplotlib
pylab.plot(args)
我想制作动画。在每一帧中,我希望同时包含一个用 mayavi 绘制的图和一个用 matplotlib 绘制的图。
mlab.pipeline.iso_surface(source, some other superfluous args)
我已经有了分别绘制这两个图的脚本,但我不知道怎么把它们合并成一个图。我希望最终的结果是一个脚本,里面包含我现在拥有的两个脚本的代码。
2 个回答
1
在这里补充一下DrV的回答,这对我帮助很大。你可以使用mlab图形来设置截图前的分辨率,比如在批量绘图的时候:
mfig = mlab.figure(size=(1024, 1024))
src = mlab.pipeline.scalar_field(field_3d_numpy_array)
mlab.pipeline.iso_surface(src)
iso_surface_plot = mlab.screenshot(figure=mfig, mode='rgba', antialiased=True)
mlab.clf(mfig)
mlab.close()
# Then later in a matplotlib fig:
plt.imshow(iso_surface_plot)
4
据我所知,目前没有直接的方法,因为这两种工具的后台架构差别很大。看起来无法把 matplotlib
的坐标轴添加到 mayavi.figure
中,反之亦然。
不过,有一种“变通的方法”,可以使用 mlab.screenshot
。
import mayavi.mlab as mlab
import matplotlib.pyplot as plt
# create and capture a mlab object
mlab.test_plot3d()
img = mlab.screenshot()
mlab.close()
# create a pyplot
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.plot([0,1], [1,0], 'r')
# add the screen capture
ax2 = fig.add_subplot(122)
ax2.imshow(img)
ax2.set_axis_off()
不过,这种方法不一定是最好的,可能还会遇到分辨率的问题(可以检查一下 mayavi
窗口的大小)。不过在大多数情况下,这个方法还是能完成任务的。