How can I hide the axes in matplotlib 3d?

后端 未结 2 1242
无人共我
无人共我 2020-12-30 19:45

How can I make a 3D plot without showing the axes?

When plotting a 3d plot, Matplotlib not only draws the x, y, and z axes, it draws light gray grids on the x-y, y-z

相关标签:
2条回答
  • 2020-12-30 20:11

    Ben Root provided a patch that fixes this for 1.0.1. It can be found as an attachment to the last email of this thread. To quote Ben:

    Ok, looks like the hiding of the 3d axes was a feature added after the v1.0 release (but before I started working on mplot3d). This patch should enable the basic feature without interfering with existing functions. To hide the axes, you would have to set the private member "_axis3don" to False, like so:

    ax = plt.gca(projection='3d') ax._axis3don = False

    If you do it this way, then you will get what you want now, and your code will still be compatible with mplot3d when you upgrade (although the preferred method would be to call set_axis_on() or set_axis_off()).

    I hope that helps!

    Ben Root

    0 讨论(0)
  • 2020-12-30 20:38

    ax.set_axis_off()

    Just to provide a concrete and direct example of what was mentioned at https://stackoverflow.com/a/7363931/895245

    #!/usr/bin/env python3
    
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.patches import Circle
    import mpl_toolkits.mplot3d.art3d as art3d
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_axis_off()
    
    # Draw a circle on the x=0 'wall'
    p = Circle((0, 0), 1, fill=False)
    ax.add_patch(p)
    art3d.pathpatch_2d_to_3d(p, zdir="x")
    p = Circle((0, 0), 1, fill=False)
    ax.add_patch(p)
    art3d.pathpatch_2d_to_3d(p, zdir="z")
    
    ax.set_xlim(-1.2, 1.2)
    ax.set_ylim(-1.2, 1.2)
    ax.set_zlim(-1.2, 1.2)
    
    plt.savefig('main.png', format='png', bbox_inches='tight')
    

    Output:

    Without ax.set_axis_off() it would look like:

    You will notice however that this produces an excessively large whitespace margin around the figure, and bbox_inches='tight' did not help as it does in 2D. How to solve that at: Remove white spaces in Axes3d (matplotlib)

    Tested on matplotlib==3.2.2.

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