将mayavi和matplotlib组合在同一图中

时间:2014-07-15 21:06:35

标签: python matplotlib visualization mayavi

我将制作动画。在每个帧中,我想要包含用

获得的mayavi图
mlab.pipeline.iso_surface(source, some other superfluous args)

和使用简单

获得的matplotlib图
pylab.plot(args)

我有脚本分别做两个,但不知道如何将它们组合成一个图。我希望最终产品是一个脚本,其中包含我目前拥有的两个脚本中的代码。

2 个答案:

答案 0 :(得分:2)

AFAIK,没有直接的方式,因为使用的后端是如此不同。似乎无法将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()

enter image description here

这不一定是最好的做事方式,也可能遇到解决问题(检查mayavi窗口的大小)。但是,它在大多数情况下完成了工作。

答案 1 :(得分:0)

添加了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)