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

Python legend with GetDist tool : push a part of second line to the right while keepin the other part to the left

我使用 Python GetDist 工具可用:GetDist tool

我的问题很简单。我想将图例第二行的一部分向右推。目前,默认情况下我有以下行为:

我想将部分文本“a = 300”的位置向右修改,与第一行处于同一水平水平,即与“a = 200”处于同一“水平” “第一行,然后在“考虑标准”和“a = 300”之间创建一个 space:这就是我想要得到的。

目前我还没有找到可以执行此操作的函数或处理此规范的简单选项。

更新

我尝试了 @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))

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

我打印了 shift 的值:shift = 898.1568

也许这个值太大了。如何只将“a = xxx”部分推到图例右侧?

这是你想要的吗?

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))