如何改善matplotlib散点图的标签位置(代码,算法,提示)? [英] How to improve the label placement for matplotlib scatter chart (code,algorithm,tips)?

查看:175
本文介绍了如何改善matplotlib散点图的标签位置(代码,算法,提示)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用matplotlib绘制散点图:

I use matplotlib to plot a scatter chart:

并根据这是代码:

if show_annote:
    for i in range(len(x)):
        annote_text = annotes[i][0][0]  # STK_ID
        ax.annotate(annote_text, xy=(x[i], y[i]), xytext=(-10,3),
            textcoords='offset points', ha='center', va='bottom',
            bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.2),
            fontproperties=ANNOTE_FONT) 

及其结果图:

但是在减少重叠方面仍有改进的余地(例如,标签盒偏移量固定为(-10,3)).是否有可以做到的算法?

But there is still room for improvement to reduce overlap (for instance the label box offset is fixed as (-10,3)). Are there algorithms that can:

  1. 根据周围居民的拥挤程度动态更改标签盒的偏移量
  2. 动态地远程放置标签框,并在气泡和标签框之间添加一条箭头线
  3. 某种程度上改变了标签方向
  4. label_box重叠泡泡比label_box重叠label_box好吗?

我只是想让人眼易于理解该图表,因此可以进行一些重叠,而不像

I just want to make the chart easy for human eyes to comprehand, so some overlap is OK, not as rigid a constraint as http://en.wikipedia.org/wiki/Automatic_label_placement suggests. And the bubble quantity within the chart is less than 150 most of the time.

我找到了所谓的Force-based label placement http://bl.ocks.org/MoritzStefaner/1377729 非常有趣.我不知道是否有任何python代码/软件包可用于实现该算法.

I find the so called Force-based label placement http://bl.ocks.org/MoritzStefaner/1377729 is quite interesting. I don't know if there is any python code/package available to implement the algorithm.

我不是一个学术专家,也不在寻找最佳解决方案,我的python代码需要标记很多图表,因此速度/内存在考虑范围之内.

I am not an academic guy and not looking for an optimum solution, and my python codes need to label many many charts, so the the speed/memory is in the scope of consideration.

我正在寻找一种快速有效的解决方案.任何有关此主题的帮助(代码,算法,技巧,思想)?谢谢.

I am looking for a quick and effective solution. Any help (code,algorithm,tips,thoughts) on this subject? Thanks.

推荐答案

使用我的库adjustText的另一个选项,是为此目的专门编写的( https://github.com/Phlya/adjustText ).

Another option using my library adjustText, written specially for this purpose (https://github.com/Phlya/adjustText).

from adjustText import adjust_text
np.random.seed(2016)

N = 50
scatter_data = np.random.rand(N, 3)
fig, ax = plt.subplots()
ax.scatter(scatter_data[:, 0], scatter_data[:, 1],
           c=scatter_data[:, 2], s=scatter_data[:, 2] * 150)
labels = ['ano_{}'.format(i) for i in range(N)]
texts = []
for x, y, text in zip(scatter_data[:, 0], scatter_data[:, 1], labels):
    texts.append(ax.text(x, y, text))
plt.show()

np.random.seed(2016)

N = 50
scatter_data = np.random.rand(N, 3)
fig, ax = plt.subplots()
ax.scatter(scatter_data[:, 0], scatter_data[:, 1],
           c=scatter_data[:, 2], s=scatter_data[:, 2] * 150)
labels = ['ano_{}'.format(i) for i in range(N)]
texts = []
for x, y, text in zip(scatter_data[:, 0], scatter_data[:, 1], labels):
    texts.append(ax.text(x, y, text))
adjust_text(texts, force_text=0.05, arrowprops=dict(arrowstyle="-|>",
                                                    color='r', alpha=0.5))
plt.show()

它不会从气泡中排斥,而只是从气泡的中心和其他文字中排斥.

It doesn't repel from the bubbles, only from their centers and other texts.

这篇关于如何改善matplotlib散点图的标签位置(代码,算法,提示)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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