Matplotlib重叠注释

Ban*_*Gap 36 python matplotlib

我想用一些文本注释图形中的条形,但如果条形图靠近并具有相当的高度,则注释高于ea.其他因而难以阅读(注释的坐标取自条形位置和高度).

如果发生碰撞,有没有办法改变其中一个?

编辑:条纹非常薄,有时非常接近,所以只是垂直对齐并不能解决问题...

图片可能会澄清一些事情: 条形图案

fra*_*xel 44

我编写了一个快速解决方案,它针对所有其他注释检查每个注释位置与默认边界框.如果发生碰撞,它会将其位置更改为下一个可用的无碰撞位置.它还放入漂亮的箭头.

对于一个相当极端的例子,它会产生这个(没有数字重叠): 在此输入图像描述

而不是这个: 在此输入图像描述

这是代码:

import numpy as np
import matplotlib.pyplot as plt
from numpy.random import *

def get_text_positions(x_data, y_data, txt_width, txt_height):
    a = zip(y_data, x_data)
    text_positions = y_data.copy()
    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
                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(x_data, y_data, text_positions, axis,txt_width,txt_height):
    for x,y,t in zip(x_data, y_data, text_positions):
        axis.text(x - txt_width, 1.01*t, '%d'%int(y),rotation=0, color='blue')
        if y != t:
            axis.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)
Run Code Online (Sandbox Code Playgroud)

以下是生成这些图的代码,显示了用法:

#random test data:
x_data = random_sample(100)
y_data = random_integers(10,50,(100))

#GOOD PLOT:
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.bar(x_data, y_data,width=0.00001)
#set the bbox for the text. Increase txt_width for wider text.
txt_height = 0.04*(plt.ylim()[1] - plt.ylim()[0])
txt_width = 0.02*(plt.xlim()[1] - plt.xlim()[0])
#Get the corrected text positions, then write the text.
text_positions = get_text_positions(x_data, y_data, txt_width, txt_height)
text_plotter(x_data, y_data, text_positions, ax2, txt_width, txt_height)

plt.ylim(0,max(text_positions)+2*txt_height)
plt.xlim(-0.1,1.1)

#BAD PLOT:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x_data, y_data, width=0.0001)
#write the text:
for x,y in zip(x_data, y_data):
    ax.text(x - txt_width, 1.01*y, '%d'%int(y),rotation=0)
plt.ylim(0,max(text_positions)+2*txt_height)
plt.xlim(-0.1,1.1)

plt.show()
Run Code Online (Sandbox Code Playgroud)


Yan*_*ann 7

一种选择是旋转文本/注释,该注释由rotation关键字/属性设置.在下面的示例中,我将文本旋转90度以确保它不会与相邻文本冲突.我还设置了va(short for verticalalignment)关键字,以便文本显示在栏上方(我用来定义文本的点上方):

import matplotlib.pyplot as plt

data = [10, 8, 8, 5]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(range(4),data)
ax.set_ylim(0,12)
# extra .4 is because it's half the default width (.8):
ax.text(1.4,8,"2nd bar",rotation=90,va='bottom')
ax.text(2.4,8,"3nd bar",rotation=90,va='bottom')

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果如下图:

在此输入图像描述

以编程方式确定各种注释之间是否存在冲突是一个棘手的过程.这可能值得一个单独的问题:Matplotlib文本维度.

  • 如果我必须手动完成,我可以简单地将其打印出来并手动添加文本.由于我需要改变杆位置和高度的几个图,这是不可行的(除了有几十个柱的事实) (3认同)

Phl*_*lya 6

使用我的库的另一个选项adjustText,专门为此目的编写(https://github.com/Phlya/adjustText).我认为它可能明显慢于接受的答案(它在很多条形图中显着减慢),但更通用和可配置.

from adjustText import adjust_text
np.random.seed(2017)
x_data = np.random.random_sample(100)
y_data = np.random.random_integers(10,50,(100))

f, ax = plt.subplots(dpi=300)
bars = ax.bar(x_data, y_data, width=0.001, facecolor='k')
texts = []
for x, y in zip(x_data, y_data):
    texts.append(plt.text(x, y, y, horizontalalignment='center', color='b'))
adjust_text(texts, add_objects=bars, autoalign='y', expand_objects=(0.1, 1),
            only_move={'points':'', 'text':'y', 'objects':'y'}, force_text=0.75, force_objects=0.1,
            arrowprops=dict(arrowstyle="simple, head_width=0.25, tail_width=0.05", color='r', lw=0.5, alpha=0.5))
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果我们允许沿x轴自动对齐,它会变得更好(我只需要解决一个小问题,它不喜欢将标签放在点上方而不是侧面......).

np.random.seed(2017)
x_data = np.random.random_sample(100)
y_data = np.random.random_integers(10,50,(100))

f, ax = plt.subplots(dpi=300)
bars = ax.bar(x_data, y_data, width=0.001, facecolor='k')
texts = []
for x, y in zip(x_data, y_data):
    texts.append(plt.text(x, y, y, horizontalalignment='center', size=7, color='b'))
adjust_text(texts, add_objects=bars, autoalign='xy', expand_objects=(0.1, 1),
            only_move={'points':'', 'text':'y', 'objects':'y'}, force_text=0.75, force_objects=0.1,
            arrowprops=dict(arrowstyle="simple, head_width=0.25, tail_width=0.05", color='r', lw=0.5, alpha=0.5))
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(当然,我必须在这里调整一些参数)