Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

python - Remove white spaces in Axes3d (matplotlib)

I am plotting a surface with bunch of polygons. The plotting is quite simple as shown below.

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

The image i get is like below. Big white space with tiny figure. I want the figure to cover most of the space. How can I achieve that ?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Several ways you can modify the whitespace:

  1. Reduce the whitespace inside the axes. To do this, you could modify the x, y and z limits using:

    ax.set_xlim()
    ax.set_ylim()
    ax.set_zlim()
    
  2. Reduce the whitespace outside the axes. To do this, you can use:

    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    
  3. Finally, you could just save a portion of the figure when you call savefig. You can modify this area using the bbox_inches kwarg, by using an actual Bbox rather than setting it to tight.

For example, lets consider this image from the matplotlib gallery. Note that I've change the axis and figure background colours, so they show up clearly on the page below.

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', facecolor='#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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...