如何间隔重叠注释
我想用一些文本注释图表中的条形,但如果条形靠得很近并且高度相当,则注释高于 ea。其他,因此难以阅读(注释的坐标取自条形位置和高度)。
如果发生碰撞,有没有办法移动其中一个?
编辑:条形非常细,有时非常接近,因此仅垂直对齐并不能解决问题...
一张图片可能会澄清一些事情:
I want to annotate the bars in a graph with some text but if the bars are close together and have comparable height, the annotations are above ea. other and thus hard to read (the coordinates for the annotations were taken from the bar position and height).
Is there a way to shift one of them if there is a collision?
Edit: The bars are very thin and very close sometimes so just aligning vertically doesn't solve the problem...
A picture might clarify things:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我编写了一个快速解决方案,它根据所有其他注释的默认边界框检查每个注释位置。如果发生碰撞,它会将其位置更改到下一个可用的无碰撞位置。它还添加了漂亮的箭头。
对于一个相当极端的例子,它将产生这样的结果(没有一个数字重叠):

而不是这样:

这是代码:
这是生成这些图的代码,显示了用法:
I've written a quick solution, which checks each annotation position against default bounding boxes for all the other annotations. If there is a collision it changes its position to the next available collision free place. It also puts in nice arrows.
For a fairly extreme example, it will produce this (none of the numbers overlap):

Instead of this:

Here is the code:
Here is the code producing these plots, showing the usage:
另一种选择是使用我的库
adjustText
,专门为此目的编写的 (https://github.com /Phlya/adjustText)。我认为它可能比接受的答案要慢得多(它会因为很多条而显着减慢),但更通用和可配置。如果我们允许沿 x 轴自动对齐,它会变得更好(我只需要解决一个小问题,它不喜欢将标签放在点上方而不是放在侧面......) 。
(当然,我必须在这里调整一些参数)
Another option using my library
adjustText
, written specially for this purpose (https://github.com/Phlya/adjustText). I think it's probably significantly slower that the accepted answer (it slows down considerably with a lot of bars), but much more general and configurable.If we allow autoalignment along x axis, it gets even better (I just need to resolve a small issue that it doesn't like putting labels above the points and not a bit to the side...).
(I had to adjust some parameters here, of course)
一种选择是旋转文本/注释,这是由
rotation
关键字/属性设置的。在下面的示例中,我将文本旋转 90 度,以保证它不会与相邻文本发生冲突。我还设置了va
(verticalalignment
的缩写)关键字,以便文本显示在栏上方(在我用来定义文本的点上方):如下图所示:
以编程方式确定各种注释之间是否存在冲突是一个比较棘手的过程。这可能值得一个单独的问题:Matplotlib 文本尺寸。
One option is to rotate the text/annotation, which is set by the
rotation
keyword/property. In the following example, I rotate the text 90 degrees to guarantee that it wont collide with the neighboring text. I also set theva
(short forverticalalignment
) keyword, so that the text is presented above the bar (above the point that I use to define the text):The result is the following figure:
Determining programmatically if there are collisions between various annotations is a trickier process. This might be worth a separate question: Matplotlib text dimensions.
只是想我会提供一个我刚刚创建的替代解决方案 textalloc 确保文本框尽可能避免彼此重叠和线路重叠,并且速度很快。
对于这个例子,你可以使用这样的东西:
这会导致
Just thought I would provide an alternative solution that I just created textalloc that makes sure that text-boxes avoids overlap with both each other and lines when possible, and is fast.
For this example you could use something like this:
This results in this