使用GetDist工具的Python图例:将第二行的一部分向右推,同时将另一部分向左推

2024-05-17 07:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的Python GetDist工具位于:GetDist tool

我的问题很简单。我想把传说第二行的一部分推到右边。目前,我默认有以下行为:

2 lines of legend pushed to the left

我想修改右边部分文本“a = 300”的位置,在第一行的同一水平面上,即在第一行的“a = 200”的同一水平面上,然后在“考虑到的标准”和“a=300”之间创建一个空格:这就是我想要得到的

我目前还没有找到一个函数可以执行此操作,也没有一个简单的选项来处理此规范

更新

我尝试了@ted930511的更新版本,即:

# g.settings
g = plots.get_subplot_plotter()

# Push Fom to the right into legend
renderer = g.fig.canvas.get_renderer()
shift = max([t.get_window_extent(renderer).width for t in g.legend.get_texts()])
for t in g.legend.get_texts():
    t.set_ha('right') # ha is alias for horizontalalignment
    t.set_position((shift,0))

但它被推向了右边太多。结果如下:

Too much pushed to the right

我已经打印了shift的值:shift = 898.1568

可能这个值太大了。如何仅将零件“a=xxx”推到图例右侧


Tags: 工具in文本rightforgetshift水平
1条回答
网友
1楼 · 发布于 2024-05-17 07:16:29

这是你想要的吗

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

fontP = FontProperties()
fontP.set_size('xx-small')

fig = plt.figure(figsize=(7.2, 7.2/2))
ax = fig.add_axes([0.1, 0.1, 0.3, 0.8])
p1, = ax.plot([1, 2, 3], label='Opt. Flat. No Gamma. - cross - standard situation - Criterion taken into account a=200')
p2, = ax.plot([3, 2, 1], label='Pess. Flat. No Gamma. - Criterion taken into account                                            a=200')
legend = ax.legend(handles=[p1, p2], bbox_to_anchor=(1.05, 1), loc='upper left', prop=fontP)

#renderer = fig.canvas.get_renderer()
#shift = max([t.get_window_extent(renderer).width for t in legend.get_texts()])
#for t in legend.get_texts():
#    t.set_ha('right') # ha is alias for horizontalalignment
#    t.set_position((shift,0))

enter image description here

相关问题 更多 >