如何修复绘图和图例之间的空间,以便新文本不会更改间距?

时间:2022-04-23 06:06:43

To be more specific, How can I fix the space between a plot and legend such that if more text is included the legend won't overlap the plot?

更具体地说,如何修复绘图和图例之间的空间,以便如果包含更多文本,图例将不会与图重叠?

For example, if you look at the plots below, when I add more text from "back now"(Plot 1) to "let's break hu"(Plot 2) in the first legend entry -- the legend extends to the left where it begins to cover the plot.

例如,如果您查看下面的图表,当我在第一个图例条目中将“back now”(图1)中的更多文本添加到“let's break hu”(图2)时,图例会延伸到左边的位置开始覆盖情节。

Is there a way to keep the space between the plot and the legend fixed? So that when there is more text the figure extends to the right rather than onto the plot itself.

有没有办法保持情节和图例之间的空间固定?因此,当有更多文本时,图形向右延伸而不是在图表本身上。

Plot 1, Plot 2

情节1,情节2

Code used for the legend:

用于图例的代码:

    lgd = ax.legend( patches, lgnd, loc="center right", bbox_to_anchor=(1.25, 0.5), prop=font )

1 个解决方案

#1


1  

Part of the answer you're looking for depends on how you made the legend and placed them in the first place. That's why people emphasize "Minimal, Complete, and Verifiable example".

您正在寻找的部分答案取决于您如何制作传奇并将其置于首位。这就是人们强调“最小,完整和可验证的例子”的原因。

To give you a simple introduction, you can control legend location using bbox_to_anchor, which accepts tuple of floats. Since you want to put the legend on the right, I would suggest using bbox_to_anchor with loc=2. This setting comes from bbox_transform. A simplified way to understand it is: bbox_to_anchor defines the relative location of the corner of the legend box and which corner of the 4 is defined by loc.

为了给您一个简单的介绍,您可以使用bbox_to_anchor控制图例位置,它接受浮点元组。由于你想把图例放在右边,我建议使用bbox_to_anchor和loc = 2。此设置来自bbox_transform。理解它的简单方法是:bbox_to_anchor定义图例框角落的相对位置,4的哪个角由loc定义。

In the following example, it puts the "upper left" corner of the legend box at (1, 1) which is the upper right corner of the plot ((0,0) is the "lower left" corner of the plot). And to make it clear, I set borderaxespad to 0.

在下面的示例中,它将图例框的“左上角”放在(1,1)(图表的右上角)((0,0)是图的“左下角”)。为了说清楚,我将borderaxespad设置为0。

import matplotlib.pyplot as plt

plt.plot([1,2,3], label="test1")
plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)

plt.show()

如何修复绘图和图例之间的空间,以便新文本不会更改间距?

#1


1  

Part of the answer you're looking for depends on how you made the legend and placed them in the first place. That's why people emphasize "Minimal, Complete, and Verifiable example".

您正在寻找的部分答案取决于您如何制作传奇并将其置于首位。这就是人们强调“最小,完整和可验证的例子”的原因。

To give you a simple introduction, you can control legend location using bbox_to_anchor, which accepts tuple of floats. Since you want to put the legend on the right, I would suggest using bbox_to_anchor with loc=2. This setting comes from bbox_transform. A simplified way to understand it is: bbox_to_anchor defines the relative location of the corner of the legend box and which corner of the 4 is defined by loc.

为了给您一个简单的介绍,您可以使用bbox_to_anchor控制图例位置,它接受浮点元组。由于你想把图例放在右边,我建议使用bbox_to_anchor和loc = 2。此设置来自bbox_transform。理解它的简单方法是:bbox_to_anchor定义图例框角落的相对位置,4的哪个角由loc定义。

In the following example, it puts the "upper left" corner of the legend box at (1, 1) which is the upper right corner of the plot ((0,0) is the "lower left" corner of the plot). And to make it clear, I set borderaxespad to 0.

在下面的示例中,它将图例框的“左上角”放在(1,1)(图表的右上角)((0,0)是图的“左下角”)。为了说清楚,我将borderaxespad设置为0。

import matplotlib.pyplot as plt

plt.plot([1,2,3], label="test1")
plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)

plt.show()

如何修复绘图和图例之间的空间,以便新文本不会更改间距?