简体   繁体   English

Matplotlib Contour Clabel位置

[英]Matplotlib Contour Clabel Location

I would like to control the location of matplotlib clabels on a contour plot, but without utilizing the manual=True flag in clabel. 我想在等高线图上控制matplotlib clabels的位置,但不使用clabel中的manual = True标志。 For example, I would like to specify an x-coordinate, and have labels created at the points that pass through this line. 例如,我想指定一个x坐标,并在通过该行的点处创建标签。 I see that you can get the location of the individual labels using get_position(), but I am stuck at that. 我看到你可以使用get_position()来获取各个标签的位置,但我仍然坚持这一点。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Yes, there now is a way to control label locations! 是的,现在有办法控制标签位置! https://github.com/matplotlib/matplotlib/pull/642 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)

No, there is no way built into matplotlib to do that. 不,matplotlib没有办法做到这一点。 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 . 假设你有一个轮廓c你可以在c.collections找到轮廓线。 For every contour line invoke get_paths and place your text on that path. 对于每个轮廓线,调用get_paths并将文本放在该路径上。

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. 另一种选择是替换手动放置的代码(在matplotlib.contour.BlockingContourLabeler )或调整找到标签位置的代码(在matplotlib.contour.locate_label ),但两个函数都非常密集。 If you can come up with a working replacement for locate_label just overwrite the old method in your plotting macro 如果你能找到一个有效的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()

Btw, if you use ipython you can easily view the function source from your interactive session with 顺便说一句,如果您使用ipython您可以轻松地从交互式会话中查看功能源

%psource c.clabel

or directly invoke your $EDITOR on the file were it is defined with 或者直接在文件上调用你的$EDITOR

%edit c.clabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM