问题描述
我正在绘制带有一堆多边形的表面.绘图非常简单,如下所示.
def plotSurface(cell,numOfLayer,name = None,alpha = 0.5):#导入库从mpl_toolkits.mplot3d导入Axes3D将Matplotlib导入为mpl从 mpl_toolkits.mplot3d.art3d 导入 Poly3DCollection将numpy导入为np导入matplotlib.pyplot作为plt#剧情的界限radius =(numOfLayer> 1)*(np.sqrt(3.)*(numOfLayer-1)-Length)+ Length#要投影的圆的半径#绘图部分fig = plt.figure(frameon=False,figsize=(12,10))ax = Axes3D(图)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')##遍历单元格##对于这里发生的事情:顶点是多边形顶点#添加到3D图ax.add_collection3d(Poly3DCollection(verts,alpha = alpha))如果名称==无:#绘制图形plt.show()别的:plt.savefig(名称,bbox_inches='tight')返回
我得到的图像如下.巨大的空白与微小的身影.我希望该图能覆盖大部分空间.我该如何实现?
修改空格的几种方法:
-
减少轴内的空白.为此,您可以使用以下方法修改
x
,y
和z
限制:ax.set_xlim()ax.set_ylim()ax.set_zlim()
减少轴外的空白.为此,您可以使用:
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
最后,您可以在调用
savefig
时保存图形的一部分.您可以使用bbox_inches
kwarg 修改此区域,使用实际的Bbox
而不是将其设置为tight
.
例如,让我们考虑来自
#上面的步骤1:更改轴限制ax.set_xlim(-8, 8)ax.set_ylim(-8,8)ax.set_zlim(-8, 8)plt.savefig('3d_whitespace1.png',facecolor ='#AAAAAA')
#上面的步骤2:更改子图边距fig.subplots_adjust(left = 0,right = 1,bottom = 0,top = 1)plt.savefig('3d_whitespace2.png', facecolor='#AAAAAA')
# 上面第3步:只保存图的一部分.在这里,我们将切一英寸#在图形的每一侧关闭,将10in x 8in图形更改为8in x 6inbbox =图bbox_inches.from_bounds(1、1、8、6)plt.savefig('3d_whitespace3.png',bbox_inches = bbox,facecolor ='#AAAAAA')
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 ?
Several ways you can modify the whitespace:
Reduce the whitespace inside the axes. To do this, you could modify the
x
,y
andz
limits using:ax.set_xlim() ax.set_ylim() ax.set_zlim()
Reduce the whitespace outside the axes. To do this, you can use:
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
Finally, you could just save a portion of the figure when you call
savefig
. You can modify this area using thebbox_inches
kwarg, by using an actualBbox
rather than setting it totight
.
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', 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')
# 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')
# 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')
# 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')
这篇关于删除 Axes3d 中的空格(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!