在同一图中结合mayavi和matplotlib

3

我将制作动画。在每一帧中,我希望包含一个使用Mayavi获得的绘图。

mlab.pipeline.iso_surface(source, some other superfluous args)

并且可以使用简单的方法获得一个使用matplotlib绘制的图表

pylab.plot(args)

我有单独实现这两个脚本的代码,但不知道如何将它们合并成一个图形。我希望最终的产品是一个包含当前已有两个脚本代码的脚本。

2个回答

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()

enter image description here

这并不一定是最好的方法,你可能会遇到分辨率问题(检查mayavi窗口的大小)。但是,在大多数情况下,它可以完成工作。


你知道如何提高截屏分辨率吗? - Dennis
@Dennis:我知道的唯一方法是将Mayavi绘图窗口尽可能地放大。当然,这要在分辨率尽可能高的屏幕上进行操作。(是的,我也希望能够从Mayavi获得真正的矢量输出到matplotlib中。) - DrV
@Dennis 我添加了一个带有代码的答案,您可以添加以设置分辨率。 - DMTishler

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接