Matplotlib轮廓Clabel位置 [英] Matplotlib Contour Clabel Location

查看:397
本文介绍了Matplotlib轮廓Clabel位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制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!

推荐答案

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

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.

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

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.

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

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()

顺便说一句,如果使用 ipython ,则可以轻松地从交互式会话中使用

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

%psource c.clabel

或直接在文件上调用您的 $ EDITOR ,该文件的定义是

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

%edit c.clabel

这篇关于Matplotlib轮廓Clabel位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆