Matplotlib 轮廓 C 标签位置
我想控制等值线图上 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,现在有一种方法可以控制标签位置!
https://github.com/matplotlib/matplotlib/pull/642
Yes, there now is a way to control label locations!
https://github.com/matplotlib/matplotlib/pull/642
不,matplotlib 中没有内置方法可以做到这一点。您应该要么使用默认位置,要么使用手动和鼠标进行完全交互。
您可能希望将其作为错误报告提交给上游,以便他们可以改进算法。
有多种选择可以解决这个问题。第一个是以编程方式将文本放置在轮廓图上。您将无法通过这种方式可靠地删除文本下方的线条。假设您有一个轮廓
c
,您可以在c.collections
中找到轮廓线。对于每条轮廓线,调用get_paths
并将文本放置在该路径上。另一种选择是替换手动放置的代码(在
matplotlib.contour.BlockingContourLabeler
中)或调整查找标签位置的代码(在matplotlib.contour.locate_label
中) ),但这两个函数都非常密集。如果您可以想出一个替代locate_label
的方法,只需覆盖绘图宏中的旧方法顺便说一句,如果您使用
ipython
您可以轻松地从交互式中查看函数源会话或直接调用您定义的文件上的
$EDITOR
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 inc.collections
. For every contour line invokeget_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 (inmatplotlib.contour.locate_label
), but both functions are pretty dense. If you can come up with a working replacement forlocate_label
just overwrite the old method in your plotting macroBtw, if you use
ipython
you can easily view the function source from your interactive session withor directly invoke your
$EDITOR
on the file were it is defined with