Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GetDist工具的Python图例:将第二行的一部分向右推,同时将另一部分向左推_Python_Matplotlib_Legend_Text Alignment - Fatal编程技术网

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

使用GetDist工具的Python图例:将第二行的一部分向右推,同时将另一部分向左推,python,matplotlib,legend,text-alignment,Python,Matplotlib,Legend,Text Alignment,我使用PythonGetDist工具,该工具可从以下站点获得: 我的问题很简单。我想把传说第二行的一部分推到右边。目前,我默认有以下行为: 我想修改右边部分文本“a=300”的位置,在第一行的同一水平面上,即在第一行的“a=200”的同一水平面上,然后在“考虑到的标准”和“a=300”之间创建一个空格:这就是我想要得到的 我目前还没有找到一个函数可以执行此操作,也没有一个简单的选项来处理此规范 更新 我尝试了更新版本的@ted930511,即: # g.settings g = plots.g

我使用Python
GetDist
工具,该工具可从以下站点获得:

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

我想修改右边部分文本“
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))
但它被推向了右边太多。结果如下:

我已经打印了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))

谢谢你的回答。你所做的几乎就是我想要的。我想要相同的第二行,但是带有文本
Pess。平的未考虑伽马-标准
在左侧对齐(靠近橙色条)文本
a=200
推到右边,就像您当前的图形:这将在两个部分之间创建一个空格,但这是我希望得到的结果。谢谢,但这并不是一个很好的方法,可以自动将
a=200
推到右边,即与第一行相同的级别,例如
a=300
?我的想法是:如果你有许多类似的图,找到自动方法是好的。如果仅此图,请将其用作初始解决方案。也许以后你会得到一个更好的解决办法:)祝你好运。在某些情况下,自动程序意味着灵活性降低。这是一个权衡。谢谢你的支持。我已经添加了有关您当前解决方案的更新?如果您可以看一下,问题的作用是文本“a=xxx”右侧对齐过多。我怀疑参数值
shift
过高。当做