当bbox_inches ='tight'且pad_inches没有达到预期的效果时,修剪3D pdf空间中的白色空间(savefig)
问题描述:
下面的代码中,我生成了一个我想用pdf保存的图。我使用savefig
命令:当bbox_inches ='tight'且pad_inches没有达到预期的效果时,修剪3D pdf空间中的白色空间(savefig)
fig.savefig("Surface_I.pdf")
PDF文件包含了大量的白色空间left
,并up
:
fig.savefig("Surface_I.pdf", bbox_inches='tight')
解决方案削减了一点x
和z
信息,而不删除来自left
和top
的空间太大:
的fig.savefig("Surface_I.pdf", bbox_inches='tight', pad_inches=0.3)
解决方案增加了一些空间,固定只是删除了信息:
,但仍然存在left
和top
太多空间。
如何从pdf中删除或修剪所有此left
和top
空白区域?
注意:所有这些图片都是pdf生成的截图。
代码:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# Function:
z_fit = a0 + a1*yy + a2*xx + a3*yy**2 + a4*xx**2 + a5*xx*yy
# Parameters:
a0 = -941.487789948
a1 = 0.0146881652963
a2 = -2.53533353374e-05
a3 = -9.64343252786e-05
a4 = -2.47416662598e-08
a5 = 3.77136886946e-07
x_mesh = np.linspace(10.0000000000000, 2000.0000000000000, 20)
y_mesh = np.linspace(-4.4119598462100, 10.8557347078000, 20)
xx, yy = np.meshgrid(x_mesh, y_mesh)
# Plot the function
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(xx, yy, z_fit, color='b', alpha=0.5)
ax.set_xlabel('\nx')
ax.set_ylabel('y')
ax.set_zlabel('\nzzzzz zzz', linespacing=3)
ax.set_title('\n\nSurface I', linespacing=3)
xlabels=[0, 250, 500, 750, 1000, 1250, 1500, 1750, 2000]
ax.set_xticklabels(xlabels,rotation=90,
verticalalignment='baseline',#)#,
horizontalalignment='left')
ylabels = [-4, -2, 0, 2, 4, 6, 8, 10]
ax.set_yticklabels(ylabels,rotation=0,
verticalalignment='baseline')#,
# horizontalalignment='left')
fig.savefig("Surface_I.pdf", bbox_inches='tight', pad_inches=0.3)
答
尝试更改列表边框下面的代码(边界= [XMIN,YMIN,XMAX,YMAX]在数字设备坐标) 这是一个有点棘手,你创建一个轴AX0尺寸边框清单 ,并在其中添加3D图形的轴。 边境尝试= [-0.5,0,1.5,1]你就会明白这个名单的
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# Parameters:
a0 = -941.487789948
a1 = 0.0146881652963
a2 = -2.53533353374e-05
a3 = -9.64343252786e-05
a4 = -2.47416662598e-08
a5 = 3.77136886946e-07
x_mesh = np.linspace(10.0000000000000, 2000.0000000000000, 20)
y_mesh = np.linspace(-4.4119598462100, 10.8557347078000, 20)
xx, yy = np.meshgrid(x_mesh, y_mesh)
# Function:
z_fit = a0 + a1*yy + a2*xx + a3*yy**2 + a4*xx**2 + a5*xx*yy
def axesborder(border):
# border = [xmin,ymin,xmax,ymax]
# return [xmin,ymin,dx,dy]
return [border[0],border[1],border[2]-border[0],border[3]-border[1]]
# Plot the function
fig = plt.figure(1)
fig.set_size_inches((16,9), forward=True)
border = [-0.1,0,1.05,1]
#border = [0,0,1,1]
ax0 = fig.add_axes(axesborder(border))
ax0.set_axis_off()
ax = fig.add_axes(ax0.get_position(),projection='3d',zorder = 1)
ax.plot_surface(xx, yy, z_fit, color='b', alpha=0.5)
ax.set_xlabel('\nx')
ax.set_ylabel('y')
ax.set_zlabel('\nzzzzz zzz', linespacing=3)
ax.set_title('\n\nSurface I', linespacing=3)
xlabels=[0, 250, 500, 750, 1000, 1250, 1500, 1750, 2000]
ax.set_xticklabels(xlabels,rotation=90,
verticalalignment='baseline',#)#,
horizontalalignment='left')
ylabels = [-4, -2, 0, 2, 4, 6, 8, 10]
ax.set_yticklabels(ylabels,rotation=0,verticalalignment='baseline')
plt.show()
plt.savefig('test.pdf')
我想你想要的是设置自己的边框中所示答案由汤姆作用[这个问题](https://*.com/questions/41225293/remove-white-spaces-in-axes3d-matplotlib)。可能它甚至足以调整像[这个问题]中的子图参数(https://*.com/questions/38604100/matplotlib-3d-plot-how-to-get-rid-of-the-excessive-white-空间?noredirect = 1&LQ = 1) – ImportanceOfBeingErnest