Using Mayavi to make 3D graphs, with Matplotlib-style axes

前端 未结 1 823
滥情空心
滥情空心 2021-02-14 18:16

I\'ve been messing around with Mayavi for 3D graphing, and I can graph the scatter plot I want but can\'t seem to get the Axes to look proper. I found the following previous que

相关标签:
1条回答
  • 2021-02-14 18:47

    This is actually pretty straightforward once we get the "trick", and I have done it many times.

    The 'trick' is to generate the mayavi plot first, then transfer it into the matplotlib window where you can use all the familiar matplotlib tools to make axes with numbers, dates, arrows, or the other pieces that matplotlib provides. In this code example I'll just drop the mayavi 'copper spheres' example into a matplotlib set of axes:

    import numpy, pylab, mayavi, mayavi.mlab
    import matplotlib.pyplot as plt
    
    t = numpy.linspace(0, 4 * numpy.pi, 20)
    cos,sin = numpy.cos, numpy.sin
    
    x = sin(2 * t)
    y = cos(t)
    z = cos(2 * t)
    s = 2 + sin(t)
    mayavi.mlab.points3d(x, y, z, s, colormap="copper", scale_factor=.25)
    
    arr = mayavi.mlab.screenshot()
    fig = plt.figure(figsize=(5, 5))
    pylab.imshow(arr)
    
    plt.show()

    The result is just the mayavi plot in a matplotlib set of axes: Mayavi graphics in matplotlib axes

    The only tricky part is that you'll need to build a working knowledge of the scales and dimensions of mayavi and matplotlib to get the two to scale together if you want to produce more than one or two manually built plots. That is non-trivial but fairly well documented for both Mayavi and Matplotlib and from the sounds of things you know both so I won't belabor those points. (Now that things are in Matplotlib you can make them part of generated PDFs too or port them into Microsoft Word with python-docx)

    0 讨论(0)
提交回复
热议问题