删除Axes3d中的空格(matplotlib)

时间:2016-12-19 14:55:50

标签: python matplotlib

我正在用一堆多边形绘制一个表面。绘图非常简单,如下所示。

def plotSurface(cell, numOfLayer, name=None, alpha = 0.5):
    #import the libraries
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib as mpl
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    import numpy as np
    import matplotlib.pyplot as plt
    #limits of the plot
    radius = (numOfLayer>1)*(np.sqrt(3.)*(numOfLayer-1)-Length)+Length#the radius of circle to be projected on
    #plotting part
    fig = plt.figure(frameon=False,figsize=(12,10))
    ax = Axes3D(fig)
    ax.set_xlim((-2*radius,2*radius))
    ax.set_ylim((-2*radius,2*radius))
    ax.set_zlim((-0.5*radius,2*radius))
    ax.axis('off')
    #fig = plt.figure()
    #ax = fig.gca(projection='3d')
    ##iterating through the cell##
    for stuff happening here : verts are the polygon vertices
          #adding to 3d plot
          ax.add_collection3d(Poly3DCollection(verts,alpha = alpha))
    if name == None:#plot the figure
        plt.show()
    else:
        plt.savefig(name,bbox_inches='tight')
    return

我得到的图像如下。与微小的图的大白色空间。我希望这个数字能够覆盖大部分空间。 我怎样才能做到这一点?

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以通过几种方式修改空白:

  1. 减少轴内的空白。为此,您可以使用以下方式修改xyz限制:

    ax.set_xlim()
    ax.set_ylim()
    ax.set_zlim()
    
  2. 减少轴外的空白。为此,您可以使用:

    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    
  3. 最后,当你拨打savefig时,你可以保存一部分数字。您可以使用bbox_inches kwarg修改此区域,方法是使用实​​际的Bbox,而不是将其设置为tight

  4. 例如,我们可以考虑matplotlib gallery中的这张图片。请注意,我更改了轴和图背景颜色,因此它们会在下面的页面上清晰显示。

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure(figsize=(10,8))
    # I added a pink axis background, just so its easy to see against the white page
    ax = fig.add_subplot(111, projection='3d', axisbg='#FFAAAA')
    
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(0, np.pi, 100)
    
    x = 10 * np.outer(np.cos(u), np.sin(v))
    y = 10 * np.outer(np.sin(u), np.sin(v))
    z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
    ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
    
    ax.axis('off')
    
    # Save the original figure (using a grey background for the figure for clarity)
    plt.savefig('3d_whitespace0.png', facecolor='#AAAAAA')
    

    enter image description here

    # Step 1 above: change the axes limits
    ax.set_xlim(-8, 8)
    ax.set_ylim(-8, 8)
    ax.set_zlim(-8, 8)
    
    plt.savefig('3d_whitespace1.png', facecolor='#AAAAAA')
    

    enter image description here

    # Step 2 above: change the subplot margins
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    
    plt.savefig('3d_whitespace2.png', facecolor='#AAAAAA')
    

    enter image description here

    # Step 3 above: save only a portion of the figure. Here we will cut one inch
    # off each side of the figure, to change the 10in x 8in figure to 8in x 6in
    bbox = fig.bbox_inches.from_bounds(1, 1, 8, 6)
    
    plt.savefig('3d_whitespace3.png', bbox_inches=bbox, facecolor='#AAAAAA')
    

    enter image description here

答案 1 :(得分:1)

通过设置ch可以减少数字边距。这可能是也可能不够,取决于实际数字。

还要注意,如果将轴方面设置为相等,则fig.subplots_adjust(top=1, bottom=0, left=0, right=1)必须是平方的。 (这不是这种情况,但可能需要其他情况)。

要转动的最后一个旋钮是减小轴限制。可以将它们设置为较小的值以减少对象周围的空白区域。 例如,在原点周围绘制半径为1的球体,可以尝试将极限设置为figsize以使整个球体适合绘图。但是,这会留下很多空白区域。将限制减少到[-1,1]将使球体很好地适应图形。为了看到这种效果,我在下面的例子中打开了轴。

[-0.57,0.57]

enter image description here