【问题标题】:Set manual location of legend with matplotlib and GetDistTool使用 matplotlib 和 GetDistTool 设置图例的手动位置
【发布时间】:2020-09-11 18:04:52
【问题描述】:

我尝试手动设置由 Getdist tool 制作的主图的主要图例的位置。

下图表示来自具有联合分布的协方差矩阵的 1/2 sigma 置信水平。由 Getdist tool制作。

Getdist tool 工具允许指定图例的位置:我尝试使用legend_loc = 'upper right',但如您所见,图顶部有重叠。我想向右移动图例以避免这种重叠:有可能吗?如果是,怎么做?

生成此图的主要程序是:

g.triangle_plot([matrix1, matrix2],
                  names,
                  filled = True,
                 legend_labels = ['Opt. Flat. No Gamma. - optimistic case - cross - standard situation - Criterion taking into accound a = 200', 'Pess. Flat. No Gamma. - pessimistic case - cross - standard situation - Criterion taking into account a = 300' ],
                  legend_loc = 'upper right',
                  contour_colors = ['darkblue','red'],
                  line_args = [{'lw':2, 'color':'darkblue'},
                  {'lw':2, 'color':'red'}]
                  )

更新 1

我无法通过以下方式在图例的左下角应用移位:

g.triangle_plot([matrix1, matrix2],
                names,
                filled = True,               
                 legend_labels = ['Opt. Flat. No Gamma. - optimistic case - cross - standard situation - Criterion taking into accound a = 200', 'Pess. Flat. No Gamma. - pessimistic case - cross - standard situation - Criterion taking into account a = 300' ],
                legend_loc = 'center right',
                contour_colors = ['darkblue','red'],
                line_args = [{'lw':2, 'color':'darkblue'},
                {'lw':2, 'color':'red'}],                
                bbox_to_anchor = [0.1, 0.5]
                )

如您所见,我尝试将图例放在顶部和中间的 0.1 处。

但不幸的是,这并没有改变任何东西,我用这些参数得到了以下情节:

您可以说,使用这些参数,简单地移到顶部就足够了,但我暂时不知道如何执行此操作。

我还尝试从g.triangle_plot 中删除locbbox_to_anchor 并尝试直接执行:

g.fig.legend(loc='center right', bbox_to_anchor=[0.1, 0.1])

但最终图中未应用相同的偏移量问题。

【问题讨论】:

  • 有用的评论:在英语中,一个说“我希望问题很清楚”,而不是“我希望问题很清楚”。但是,在 Stack Overflow 上,我们热衷于解决闲聊问题 - 说您希望某事清楚并不会帮助其清晰,因此希望您不要发表这些多余的言论。

标签: python numpy matplotlib legend


【解决方案1】:

pyplot 的 legend 函数中有一个参数称为 bbox_to_anchor。这有点像相对偏差,我不确定它是如何工作的。但基本上你可以在那里设置一些水平和垂直移动的值,然后调整到你想要的位置。

例如你可以写 plt.legend(bbox_to_anchor=[2.32, 0.5], loc='center right')

【讨论】:

  • 感谢您的快速回答。不幸的是,我尝试更改 bbox_to_anchor 的值,但这不会导致图例位置发生任何变化。
  • 嗯,这很奇怪,因为这就是我通常调整图例位置的方式。也许它不适用于 GetDist,我尝试仅在 pyplot 中使用。
  • 确实,很奇怪。我认为这可能是由于生成绘图的主程序 'g.triangle_plot` 使用自己的参数legend_loc 参数。我必须补充一点,我已经定义了gby:g = plots.get_subplot_plotter()。你怎么看?
  • 我在 github 上查看了他们的代码,看起来他们使用了 bbox_to_anchor 参数 (github.com/cmbant/getdist/blob/master/getdist/plots.py#L1872)。我还从这里getdist.readthedocs.io/en/latest/plot_gallery.html 运行了一些代码,在g.add_legend 中粘贴了bbox_to_anchor 参数,它完全改变了图例的位置。所以也许你应该直接调用g.add_legend 函数。还可以尝试使用一些更高的参数(例如 [2.1, 2.5])发送bbox_to_anchor。也许位置上的微小差异并不那么明显。
  • 您使用g.add_legend 的解决方案有效,但现在,我面临另一个问题:即使我删除了legend_locbbox_to_anchor 和大部分相关内容,例程g.trianglr_plot 也会添加另一个图例到这个例程中的传奇。
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
相关资源
最近更新 更多