如何修复重叠的标注/文本

92

我正在尝试防止图表中注释文本的重叠。在Matplotlib重叠注释的被接受答案中建议的方法看起来非常有前途,但是该方法仅适用于条形图。我正在尝试将“axis”方法转换为我想要做的内容,并且我不理解文本如何对齐。

import sys
import matplotlib.pyplot as plt


# start new plot
plt.clf()
plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")

together = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)]
together.sort()

for x,y,z in together:
    plt.annotate(str(x), xy=(y, z), size=8)

eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]

p1 = plt.plot(eucs,covers,color="black", alpha=0.5)

plt.savefig("test.png")

这里可以找到图片(如果这个链接有效):这里(这是代码):

image1

还有这里的图片(更为复杂):

image2


请参见https://dev59.com/pmUp5IYBdhLWcg3w669C#15859652。 - tacaswell
5个回答

202

我只是想在这里发布另一种解决方案,一个我编写的小型库,用于实现这种功能:https://github.com/Phlya/adjustText 可以在此处查看该过程的示例: enter image description here

这是示例图片:

import matplotlib.pyplot as plt
from adjustText import adjust_text
import numpy as np
together = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)]
together.sort()

text = [x for (x,y,z) in together]
eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]

p1 = plt.plot(eucs,covers,color="black", alpha=0.5)
texts = []
for x, y, s in zip(eucs, covers, text):
    texts.append(plt.text(x, y, s))

plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")
adjust_text(texts, only_move={'points':'y', 'texts':'y'}, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
plt.show()

输入图像描述

如果您想要完美的图形,可以稍微调整一下。首先,让我们让文本避开这些线条-为此,我们只需使用scipy.interpolate.interp1d沿着它们创建许多虚拟点。

我们希望避免沿x轴移动标签,因为,好吧,为什么不为了说明目的而这样做呢。为此,我们使用参数only_move={'points':'y', 'text':'y'}. 如果我们只想在它们与文本重叠的情况下沿x轴移动它们,则使用move_only={'points':'y', 'text':'xy'}。此外,在开始时,函数会选择文本相对于其原始点的最佳对齐方式,因此我们只希望在y轴上发生自动对齐,因此为autoalign='y'。我们还降低了点之间的排斥力,以避免由于人工避免线条而使文本飞得太远。所有这些:

from scipy import interpolate
p1 = plt.plot(eucs,covers,color="black", alpha=0.5)
texts = []
for x, y, s in zip(eucs, covers, text):
    texts.append(plt.text(x, y, s))

f = interpolate.interp1d(eucs, covers)
x = np.arange(min(eucs), max(eucs), 0.0005)
y = f(x)    
    
plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")
adjust_text(texts, x=x, y=y, autoalign='y',
            only_move={'points':'y', 'text':'y'}, force_points=0.15,
            arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
plt.show()

这里输入图片描述


2
干得好,Phlya!你可能还可以将这个答案或类似的内容添加到https://dev59.com/wGox5IYBdhLWcg3wpFv5。 - naught101
1
@Phlya:根据您在GitHub上的示例,现在它可以工作了,看起来真是太棒了!由于其他人可能会先访问SO找到您的软件包,我建议您更新答案和代码。 - Ruthger Righart
1
@RuthgerRighart,很高兴它对你有用!感谢建议,我想我会在某个时候这样做... - Phlya
@gaborous 哎呀,我错过了你的评论,抱歉 - 你是什么意思?它可以在循环中工作,但它只考虑到被传递给函数的对象... - Phlya
2
如果有人遇到错误'str' object has no attribute 'values',请使用adjust_text(texts, only_move={'points':'y', 'texts':'y'}, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))代替答案中的语法。请参阅https://github.com/Phlya/adjustText/issues/83。 - Matthias Arras
显示剩余15条评论

11

这里有一个简单的解决方案:(适用于Jupyter Notebook)

%matplotlib notebook
import mplcursors

plt.plot.scatter(y=YOUR_Y_DATA, x =YOUR_X_DATA)


mplcursors.cursor(multiple = True).connect(
    "add", lambda sel: sel.annotation.set_text(
          YOUR_ANOTATION_LIST[sel.target.index]
))

右键单击一个点以显示其注释。

左键单击标注以关闭它。

右键单击并拖动标注以移动它。

enter image description here


7

经过许多调试,我终于弄清楚了。再次感谢Matplotlib overlapping annotations的答案。

但是,我不知道如何找到文本的确切宽度和高度。如果有人知道,请发布改进(或添加带有方法的评论)。

import sys
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

def get_text_positions(text, x_data, y_data, txt_width, txt_height):
    a = zip(y_data, x_data)
    text_positions = list(y_data)
    for index, (y, x) in enumerate(a):
        local_text_positions = [i for i in a if i[0] > (y - txt_height) 
                            and (abs(i[1] - x) < txt_width * 2) and i != (y,x)]
        if local_text_positions:
            sorted_ltp = sorted(local_text_positions)
            if abs(sorted_ltp[0][0] - y) < txt_height: #True == collision
                differ = np.diff(sorted_ltp, axis=0)
                a[index] = (sorted_ltp[-1][0] + txt_height, a[index][1])
                text_positions[index] = sorted_ltp[-1][0] + txt_height*1.01
                for k, (j, m) in enumerate(differ):
                    #j is the vertical distance between words
                    if j > txt_height * 2: #if True then room to fit a word in
                        a[index] = (sorted_ltp[k][0] + txt_height, a[index][1])
                        text_positions[index] = sorted_ltp[k][0] + txt_height
                        break
    return text_positions

def text_plotter(text, x_data, y_data, text_positions, txt_width,txt_height):
    for z,x,y,t in zip(text, x_data, y_data, text_positions):
        plt.annotate(str(z), xy=(x-txt_width/2, t), size=12)
        if y != t:
            plt.arrow(x, t,0,y-t, color='red',alpha=0.3, width=txt_width*0.1, 
                head_width=txt_width, head_length=txt_height*0.5, 
                zorder=0,length_includes_head=True)

# start new plot
plt.clf()
plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")

together = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)]
together.sort()

text = [x for (x,y,z) in together]
eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]

p1 = plt.plot(eucs,covers,color="black", alpha=0.5)

txt_height = 0.0037*(plt.ylim()[1] - plt.ylim()[0])
txt_width = 0.018*(plt.xlim()[1] - plt.xlim()[0])

text_positions = get_text_positions(text, eucs, covers, txt_width, txt_height)

text_plotter(text, eucs, covers, text_positions, txt_width, txt_height)

plt.savefig("test.png")
plt.show()

创建 http://i.stack.imgur.com/xiTeU.png enter image description here

现在更复杂的图形是 http://i.stack.imgur.com/KJeYW.png,仍然有些模糊但好多了! enter image description here


get_window_extent() 是您想要的艺术家函数。 - tacaswell
annotation.get_window_extent() 返回 Bbox(array([[ 349.194625, 38.0572 ], [ 372.132125, 448.0572 ]]))。这对于文本的宽度/高度意味着什么? - homebrand
2
这是文本的边界框,以显示单位为单位。请参见http://matplotlib.org/users/transforms_tutorial.html和https://dev59.com/3WUo5IYBdhLWcg3wpgsf#15883858。 - tacaswell
5
这段代码是来自于 https://dev59.com/P2ox5IYBdhLWcg3w_JLc#10739207,对吧?我认为给原始作者 @fraxel 加上署名会很好。 - deeenes
1
get_text_positions 中,似乎应该将 a[index][2] 替换为 a[index][1]。实际上,a 中的元素看起来是大小为 2 的元组。由于相关部分未被执行,因此提供的示例并没有导致代码崩溃。 - etna

0

我想再添加一个我在代码中使用的解决方案。

  1. 获取y轴刻度并找到任意两个相邻刻度之间的差异(y_diff)。
  2. 通过将图形的每个“y”元素添加到列表中来注释第一行。
  3. 在注释第二项时,检查前一个图形(prev_y)的注释是否落在相同的y轴刻度范围内(curr_y)。
  4. 仅在(prev_y - curr_y)>(y_diff / 3)时进行注释。您可以将差异除以所需的图形大小和注释字体大小的数量。
 annotation_y_values = []
    for i, j in zip(x, df[df.columns[0]]):
        annotation_y_values.append(j)
        axs.annotate(str(j), xy=(i, j), color="black")
 count = 0
 y_ticks = axs.get_yticks()
 y_diff = y_ticks[-1] - y_ticks[-2]
 for i, j in zip(x, df1[df1.columns[0]]):
        df_annotate_value = annotation_y_values[count]
        current_y_val = j
        diff = df_annotate_value - current_y_val
        if diff > (y_diff/3):
            axs.annotate(str(j), xy=(i, j), color="black", size=8)
        count = count + 1


0

刚刚为这类问题创建了一个包:textalloc

下面的示例展示了您可能如何在此情况下使用它。通过一些参数调整,您可以在几秒钟内生成像这样的绘图:

import textalloc as ta
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
x_lines = [np.array([0.0, 0.03192317, 0.04101177, 0.26085659, 0.40261173, 0.42142198, 0.87160195, 1.00349979]) + np.random.normal(0,0.03,(8,)) for _ in range(4)]
y_lines = [np.array([0. , 0.2, 0.2, 0.4, 0.8, 0.6, 1. , 1. ]) + np.random.normal(0,0.03,(8,)) for _ in range(4)]
text_lists = [['0', '25', '50', '75', '100', '125', '150', '250'] for _ in range(4)]

texts = []
for tl in text_lists:
    texts += tl
fig,ax = plt.subplots(dpi=100)
for x_line,y_line,text_list in zip(x_lines,y_lines,text_lists):
    ax.plot(x_line,y_line,color="black",linewidth=0.5)
ta.allocate_text(fig,ax,np.hstack(x_lines),np.hstack(y_lines),
            texts,
            x_lines=x_lines, y_lines=y_lines,
            max_distance=0.1,
            min_distance=0.025,
            margin=0.0,
            linewidth=0.5,
            nbr_candidates=400)
plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接