Matplotlib 轮廓 C 标签位置

发布于 2024-08-31 22:06:16 字数 152 浏览 14 评论 0原文

我想控制等值线图上 matplotlib clabel 的位置,但不使用 clabel 中的 manual=True 标志。例如,我想指定一个 x 坐标,并在穿过该线的点处创建标签。我发现您可以使用 get_position() 获取各个标签的位置,但我陷入困境。任何帮助将不胜感激。谢谢!

I would like to control the location of matplotlib clabels on a contour plot, but without utilizing the manual=True flag in clabel. For example, I would like to specify an x-coordinate, and have labels created at the points that pass through this line. I see that you can get the location of the individual labels using get_position(), but I am stuck at that. Any help would be greatly appreciated. Thanks!

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

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

发布评论

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

评论(2

狠疯拽 2024-09-07 22:06:16

是的,现在有一种方法可以控制标签位置!
https://github.com/matplotlib/matplotlib/pull/642

plt.figure()
CS = plt.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]
plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)

Yes, there now is a way to control label locations!
https://github.com/matplotlib/matplotlib/pull/642

plt.figure()
CS = plt.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]
plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)
时光瘦了 2024-09-07 22:06:16

不,matplotlib 中没有内置方法可以做到这一点。您应该要么使用默认位置,要么使用手动和鼠标进行完全交互。

您可能希望将其作为错误报告提交给上游,以便他们可以改进算法。

有多种选择可以解决这个问题。第一个是以编程方式将文本放置在轮廓图上。您将无法通过这种方式可靠地删除文本下方的线条。假设您有一个轮廓 c,您可以在 c.collections 中找到轮廓线。对于每条轮廓线,调用 get_paths 并将文本放置在该路径上。

另一种选择是替换手动放置的代码(在 matplotlib.contour.BlockingContourLabeler 中)或调整查找标签位置的代码(在 matplotlib.contour.locate_label 中) ),但这两个函数都非常密集。如果您可以想出一个替代 locate_label 的方法,只需覆盖绘图宏中的旧方法

def your_locate_label(self, linecontour, labelwidth):
    # some magic
    pass

ar = np.array([[1,0], [0,1]]
c = matplotlib.contour(ar)
c.locate_label = your_locate_label

c.clabel()

顺便说一句,如果您使用 ipython 您可以轻松地从交互式中查看函数源会话

%psource c.clabel

或直接调用您定义的文件上的 $EDITOR

%edit c.clabel

No, there is no way built into matplotlib to do that. You are supposed to either live with the default locations or go fully interactive with manual and using the mouse.

You might want to file this as a bug report upstream so they can improve their algorithms.

There are multiple options to work around this. The first one is to programmatically place text on the contour figure. You will not be able to reliably remove the lines underneath the text this way. Assuming you have a contour c you can find the contour lines in c.collections. For every contour line invoke get_paths and place your text on that path.

The other option would be to replace the code for manual placement (in matplotlib.contour.BlockingContourLabeler) or tweak the code that finds the label positions (in matplotlib.contour.locate_label), but both functions are pretty dense. If you can come up with a working replacement for locate_label just overwrite the old method in your plotting macro

def your_locate_label(self, linecontour, labelwidth):
    # some magic
    pass

ar = np.array([[1,0], [0,1]]
c = matplotlib.contour(ar)
c.locate_label = your_locate_label

c.clabel()

Btw, if you use ipython you can easily view the function source from your interactive session with

%psource c.clabel

or directly invoke your $EDITOR on the file were it is defined with

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