【问题标题】:Python plot elements not rendered properly with save to file保存到文件时 Python 绘图元素未正确呈现
【发布时间】:2015-10-26 11:09:44
【问题描述】:

我使用 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()

如何正确保存文件?

【问题讨论】:

    标签: python pdf matplotlib


    【解决方案1】:

    我不知道你为什么要先打电话给plt.savefig,然后再对这个人物做更多的事情,比如添加一个图例。我想如果你在让 pyplot 计算边界框之后调用图例,图例当然不会关心这个。

    【讨论】:

    • 丹克 schon。我的电线又被交叉了。
    猜你喜欢
    • 2021-12-11
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 2020-07-29
    • 2020-02-19
    相关资源
    最近更新 更多