有没有办法将数据放在标签前面?

发布于 2025-01-13 18:13:30 字数 407 浏览 2 评论 0原文

Matplotlib 中的标签似乎总是显示在最顶部的覆盖层上,覆盖其后面的数据。

示例:

在此处输入图像描述

该图有 2 个红色文本注释。 底部覆盖着灰色的“Meteo”文本。

有没有办法将红色注释移到灰色文本标签上方?

我正在玩 zorder 但没有成功。

It seems that the labels in Matplotlib always are shown on the most top overlay, covering the data behind it.

Example:

enter image description here

The plot has 2 text annotations in red.
The bottom one is overlayed by a gray "Meteo" text.

Is there a way to move the red annotation above the gray text label?

I was playing with the zorder but with no success.

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

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

发布评论

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

评论(2

守望孤独 2025-01-20 18:13:30

它工作正常,但不是以您期望的方式工作。

每个元素都有它的zorder。甚至 ax 也有默认的 zorder=0。这意味着,其中的所有元素(我们称之为)都将具有最终的zorder=0

但是当绘制ax layer时,zorder分别应用于每个元素,并从下到上正确地放置在视图中。
现在,您已经使用最终的zorder=0 渲染了 ax layer

然后您将使用 fig.text 添加文本。它将文本添加到图形图层。现在你有两个对象要渲染; ax layer 带有 zorder = 0text 带有 zorder=0 。您可以设置文本 zorder=-1 ,它将把文本放在 ax layer 下面,但如果 ax 则它将不可见> 有扎实的背景。尝试在 fig layer 中添加具有更高或更低 zorder 的另一个 text,它将正确绘制在顶部或位于另一个 text 元素下方。

It works properly, but not in a way You might expect.

Every element has it's zorder. Even ax has default zorder=0. It means, that all elements within that, let's call it a layer, will have final zorder=0.

But when ax layer is drawn, zorder is applied to every element respectively and are placed correctly in the view from bottom to the top.
So now You have ax layer rendered with final zorder=0.

Then You are adding text with fig.text. Which adds text to the figure layer. So now You have two objects to render; ax layer with zorder = 0 and text with zorder=0 as well. You can set text zorder=-1 which will put text below ax layer, but it will be invisible if ax has solid background. Try to add another text in the fig layer with higher or lower zorder and it will be properly drawn on top or below another text element.

烟凡古楼 2025-01-20 18:13:30

看来这只适用于 注释 但不适用于 text

下面是一个示例:

在此处输入图像描述

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects

fig, ax = plt.subplots()

# foreground annotation
a1 = ax.annotate("annotation", (0.5, 0.5), fontsize='xx-large', color="red", zorder=9)
a1.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()])

# background annotation
a2 = ax.annotate("Another annotation", (0.6, 0.48), fontsize='xx-large', color="blue", zorder=5)
a2.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()])

# zorder ignored:
t1 = fig.text(0.56, 0.49, "Background", color='gray', ha='right', zorder=0)
t1.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()])

plt.show() 

It seems this only works with annotations but not with text

Below an example:

enter image description here

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects

fig, ax = plt.subplots()

# foreground annotation
a1 = ax.annotate("annotation", (0.5, 0.5), fontsize='xx-large', color="red", zorder=9)
a1.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()])

# background annotation
a2 = ax.annotate("Another annotation", (0.6, 0.48), fontsize='xx-large', color="blue", zorder=5)
a2.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()])

# zorder ignored:
t1 = fig.text(0.56, 0.49, "Background", color='gray', ha='right', zorder=0)
t1.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()])

plt.show() 

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