使用下面的代码,我已经生成了一个图形,我希望将其保存为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)发布于 2017-07-31 06:50:56
尝试在下面的代码中更改列表边框( ax0 = xmin,ymin,xmax,ymax )创建一个大小为边框列表的轴的边框列表,然后在里面添加3d图形的轴,这有点棘手。尝试使用border = -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')发布于 2019-11-21 05:29:54
我手动裁剪生成的pdf图,如下所示:
pdfcrop --margins '0 0 0 0' input.pdf output.pdf来自pdfcrop帮助消息:
--margins "<left> <top> <right> <bottom>"
如果您需要裁剪许多绘图,您可以编写一个简单的bash脚本来自动执行该过程。这整个解决方案有点丑陋,但它是有效的。
https://stackoverflow.com/questions/45380317
复制相似问题