2016-12-19 14 views
0

ポリゴンの束でサーフェスをプロットしています。プロットは以下のように非常に簡単です。Axes3d(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

+0

あなたの軸の限界を減らしますか?例えば'ax.set_xlim(-1.1 * radius、1.1 * radius)'など – tom

+0

軸の制限はありません。私はちょうどプロットされた形状に合っています。これは解決策ではありません –

+0

'fig.subplots_adjust(左= 0、右= 1、下= 0、上= 1)'は軸の外の空白をたくさん削除します – tom

答えて

1

図マージンがfig.subplots_adjust(top=1, bottom=0, left=0, right=1)を設定することにより低減することができます。これは、実際の数字に応じて、十分でないかもしれません。

軸のアスペクトが等しい場合、figsizeを2乗する必要があります。 (これはここでは当てはまりませんが、他の場合には必要かもしれません)。

最後のノブは、軸の限界を減らすことです。これらの値は、オブジェクトの周りの空白を減らすために小さな値に設定することができます。 たとえば、半径1の球を原点の周りにプロットすると、球全体がプロットに収まるように、限度を[-1,1]に設定することができます。しかし、これは周囲に多くの空白を残します。限界を[-0.57,0.57]に減らすと、球体が図形にうまく収まるようになります。この効果を見るために、以下の例で軸をオンにしました。あなたは空白を変更することができ

import matplotlib.pyplot as plt 
from matplotlib import cm 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 

u = np.linspace(0, 2 * np.pi, 12) 
v = np.linspace(0, np.pi,15) 
x = np.outer(np.cos(u), np.sin(v)) 
y = np.outer(np.sin(u), np.sin(v)) 
z = np.outer(np.ones(np.size(u)), np.cos(v)) 
F = np.sin(x)*y + z 
F = (F-F.min())/(F-F.min()).max() 

#Set colours and render 
fig = plt.figure(figsize=(8,4)) 
fig.subplots_adjust(top=1, bottom=0, left=0, right=1, wspace=0) 
ax = fig.add_subplot(121, projection='3d') 
ax2 = fig.add_subplot(122, projection='3d') 

# plotting a sphere with radius 1. 
# Naturally, setting the limits to 1 makes sense 
ax.plot_surface(x,y,z, rstride=1, cstride=1, facecolors=cm.jet(F), alpha=0.5) 
ax.set_xlim(np.array([-1,1])) 
ax.set_ylim(np.array([-1,1])) 
ax.set_zlim(np.array([-1,1])) 

# plotting a sphere with radius 1. 
# but now reducing the limits 
ax2.plot_surface(x,y,z, rstride=1, cstride=1, facecolors=cm.jet(1-F), alpha=0.5) 
ax2.set_xlim(np.array([-1,1])*.57) 
ax2.set_ylim(np.array([-1,1])*.57) 
ax2.set_zlim(np.array([-1,1])*.57) 

#ax.axis('off') # turned on to see the effect. Turn off to have a nice image. 
plt.show() 

enter image description here

1

いくつかの方法:

  1. 軸内部の空白を減らします。これを行うには、使用してxyz制限を変更できます。

    ax.set_xlim() 
    ax.set_ylim() 
    ax.set_zlim() 
    
  2. が軸外の空白を減らします。これを行うには、使用することができます:あなたはsavefigを呼び出すとき

    fig.subplots_adjust(left=0, right=1, bottom=0, top=1) 
    
  3. 最後に、あなただけの数字の部分を救うことができます。この領域はbbox_inches kwargを使用して、実際にはBboxを使用してtightに設定することで変更できます。

たとえば、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

​​

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