限制 Matplotlib 图例最佳位置选项

发布于 2025-01-10 08:59:58 字数 675 浏览 1 评论 0原文

在 Matplotlib 中,图例有一个参数 loc ,它允许指定图例的位置。

用户可以强制图例位于 9 个不同的位置,或者让 matplotlib 决定图例的最佳位置。

来自 文档

字符串“左上”、“右上”、“左下”、“右下” 将图例放在轴/图形的相应角上。

字符串“上中心”、“下中心”、“左中心”、“中心” right' 将图例放在相应边缘的中心 轴/图。

字符串“center”将图例放置在图例的中心 轴/图。

字符串“best”将图例放置在九个位置中的位置 到目前为止定义的位置,与其他绘制的重叠最小 艺术家。

现在,我想强制图例位于图的右侧部分,但根据数据,最佳位置可能是“右上”或“右下”。

我不希望图例放置在左侧或中心,但我仍然希望计算“右上”或“右下”之间的最佳位置。

根据文档,最佳位置是通过计算与其他绘制的艺术家的最小重叠来计算的。

是否有一种方法可以限制最佳选项仅考虑某些选项而不考虑九个地点中?或者手动调用仅使用所需选项计算此重叠的函数?

In Matplotlib, the legend has a parameter loc that allows to specify the location of the legend.

The user can force the legend to be in 9 different locations or let matplotlib decide where is the best location for the Legend.

From the documentation:

The strings 'upper left', 'upper right', 'lower left', 'lower right'
place the legend at the corresponding corner of the axes/figure.

The strings 'upper center', 'lower center', 'center left', 'center
right' place the legend at the center of the corresponding edge of the
axes/figure.

The string 'center' places the legend at the center of the
axes/figure.

The string 'best' places the legend at the location, among the nine
locations defined so far, with the minimum overlap with other drawn
artists.

Now, I want to force the legend to be on the right part of the plot, but depending on the data, the best location could be 'upper right' or 'lower right'.

I don't want the legend to be placed on the left or on the center, but I still want the best location between 'upper right' or 'lower right' to be calculated.

According to the documentation the Best location is calculated by calculating the minimum overlap with other drawn artists

Is there a way of limiting the best option to take only certain options in account and not among the nine locations? Or to manually call the function that calculates this overlap with only the desired options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2025-01-17 08:59:58

您可以限制 matplotlib 考虑的 计算的区域最佳位置通过使用bbox_to_anchor 关键字:

import matplotlib.pyplot as plt
import numpy as np


fig, (ax1, ax2) = plt.subplots(2)

x = np.arange(10)
ax1.plot(x, x**2, label="best location")
ax1.legend(loc="best")    

ax2.plot(x, x**2, label="best right location")
#bbox signature (left, bottom, width, height) 
#with 0, 0 being the lower left corner and 1, 1 the upper right corner
ax2.legend(loc="best", bbox_to_anchor=(0.6, 0., 0.4, 1.0)) 

plt.show()

示例输出:

在此处输入图像描述

You can restrict the area that matplotlib considers for the calculation of the best position by using the bbox_to_anchor keyword:

import matplotlib.pyplot as plt
import numpy as np


fig, (ax1, ax2) = plt.subplots(2)

x = np.arange(10)
ax1.plot(x, x**2, label="best location")
ax1.legend(loc="best")    

ax2.plot(x, x**2, label="best right location")
#bbox signature (left, bottom, width, height) 
#with 0, 0 being the lower left corner and 1, 1 the upper right corner
ax2.legend(loc="best", bbox_to_anchor=(0.6, 0., 0.4, 1.0)) 

plt.show()

Sample output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文