使用“保存到文件”无法正确呈现Python打印元素

使用“保存到文件”无法正确呈现Python打印元素,python,pdf,matplotlib,Python,Pdf,Matplotlib,我使用matplotlib获得了以下Python绘图: 但我需要将它保存到pdf。当我这样做时,它不能正确渲染,许多元素位于错误的位置,有些甚至没有出现。在我的案例中,我搜索过但没有找到解决方法: 这是我的绘图代码: plt.plot(0, 0) plt.tick_params( axis='both', which='both', bottom='off', top='off', left='off', right='off',

我使用matplotlib获得了以下Python绘图:

但我需要将它保存到pdf。当我这样做时,它不能正确渲染,许多元素位于错误的位置,有些甚至没有出现。在我的案例中,我搜索过但没有找到解决方法:

这是我的绘图代码:

plt.plot(0, 0)
plt.tick_params(
axis='both',          
which='both',
bottom='off',      
top='off',         
left='off',      
right='off', 
labelleft='off',
labelbottom='off',
labelsize=8)

a = plt.axes([.36, .2, .6, .6])
plt.tick_params(
    axis='both',
    which='both',     
    labelsize=8)
a.spines['left'].set_position('center')
a.spines['bottom'].set_position('center')
a.spines['right'].set_color('none')
a.spines['top'].set_color('none')
a.xaxis.set_ticks_position('bottom')
a.yaxis.set_ticks_position('left')
plt.plot(x,y1, 'g--',label='m$_{1}$=' +str(mass1)+'M$_{\odot}$')
plt.plot(x,y2, 'b--',label='m$_{2}$=' +str(mass2)+'M$_{\odot}$')
plt.plot(x,y1n, 'g--')
plt.plot(x,y2n, 'b--')
plt.plot(start1[0],start1[1], 'xr',label='Position at \ntime t=0')
plt.plot(start2[0],start2[1], 'xr')
plt.plot(stop1[0],stop1[1], 'og', label='Position at \ntime t=' +     str(t) + ' yr')
plt.plot(stop2[0],stop2[1], 'og')
plt.text(-75,-10, r'Binary period ' + str(period(m1+m2,a1+a2)) + 'yr',fontsize=8)
plt.text(-75,-15, r'm$_{1}$ period ' + str(period(m1,a1)) + ' yr',fontsize=8)
plt.text(-75,-20, r'm$_{2}$ period ' + str(period(m2,a2)) + ' yr',fontsize=8)
plt.text(-75,-25, r'a$_{1}$ = ' + str(a1) + ' AU', fontsize=8)
plt.text(-75,-30, r'b$_{1}$ = ' + str(sigfigs(LOSorbit(0,a1))) + ' AU',fontsize=8)
plt.text(-75,45, 'Apparent Orbits of Circular Binary System', fontsize=8, fontweight='bold')
plt.text(-75,-35, r'a$_{2}$ = ' + str(a2) + ' AU', fontsize=8)
plt.text(-75,-40, r'b$_{2}$ = ' + str(sigfigs(LOSorbit(0,a2))) + ' AU',fontsize=8)
plt.xlabel('x /AU', fontsize=8)
h = plt.ylabel('y /AU', fontsize=8)
h.set_rotation(0)
a.xaxis.set_label_coords(1.06, 0.54)
a.yaxis.set_label_coords(0.55, 1.01)
plt.savefig('binary_sys.pdf', bbox_inches='tight')
plt.grid()
box = a.get_position()
a.set_position([box.x0, box.y0, box.width * 0.8, box.height])
a.legend(numpoints=1,loc='center left',bbox_to_anchor=(-0.46, 0.75),prop={'size':8})
plt.show()

如何正确保存该文件?

我不知道在添加图例等图形之前,为什么要调用
plt.savefig
。我想,如果在pyplot计算边界框后调用图例,那么图例当然不会在意这一点。

Danke schon。我又犯了错误。