防止标签在 matplotlib 垂直时间轴图中重叠

Prevent labels from overlapping in matplotlib vertical timeline plot

提问人:Hack-R 提问时间:8/8/2022 最后编辑:Hack-R 更新时间:8/8/2022 访问量:204

问:

我通常用于防止数据标签重叠的方法似乎不适用于这个基于 matplotlib 的垂直时间线。通常我会使用类似 .autofmt_xdate

我实际上并不关心图表需要多“高”(长),或者项目之间的垂直距离是否完美地保留了项目之间的日期间距之比。我只是不希望文本标签重叠。

我不是很有经验,但到目前为止,我已经确定控制文本标签的代码是matplotlib

_ = ax.text(label_offsets[i], d, l, ha=align, fontfamily='serif', fontweight='bold', color='royalblue',fontsize=12)

其中,时间戳用作 y 轴坐标。我尝试创建一个循环计数器和一个列表来包含每次循环迭代的值,这样我就可以在当前日期和之前的日期之间进行比较,然后将一个固定值(例如 15 天)添加到当前值,如果之前的值太接近。这似乎没有奏效。dddd

绘图代码:

chartdata = pd.read_csv(
    "data/Events.csv"
)

chartdata = chartdata.query('Year > 1939')

dates = pd.to_datetime(chartdata['Date_Clean_Approx'])
min_date = date(np.min(dates).year - 2, np.min(dates).month, np.min(dates).day)
max_date = date(np.max(dates).year + 2, np.max(dates).month, np.max(dates).day)

labels = chartdata['Name']

# labels with associated dates
labels = ['{0:%d %b %Y}:\n{1}'.format(d, l) for l, d in zip (labels, dates)]

fig, ax = plt.subplots(figsize=(6, 32), constrained_layout=True)
_ = ax.set_xlim(-20, 20)
_ = ax.set_ylim(min_date, max_date)
_ = ax.axvline(0, ymin=0.05, ymax=0.95, c='deeppink', zorder=1)

_ = ax.scatter(np.zeros(len(dates)), dates, s=120, c='palevioletred', zorder=2)
_ = ax.scatter(np.zeros(len(dates)), dates, s=30, c='darkmagenta', zorder=3)

label_offsets = np.repeat(2.0, len(dates))
label_offsets[1::2] = -2.0

for i, (l, d) in enumerate(zip(labels, dates)):
    d = d - timedelta(days=90)
    align = 'right'
    if i % 2 == 0:
        align = 'left'
    _ = ax.text(label_offsets[i], d, l, ha=align, fontfamily='serif', fontweight='bold', color='royalblue',fontsize=12)

stems = np.repeat(2.0, len(dates))
stems[1::2] *= -1.0    
x = ax.hlines(dates, 0, stems, color='darkmagenta')

# hide lines around chart
for spine in ["left", "top", "right", "bottom"]:
    _ = ax.spines[spine].set_visible(False)

# hide tick labels
_ = ax.set_xticks([])
_ = ax.set_yticks([])
_ = ax.set_title('UAP (UFO) Milestones, 1940 - Present', 
                 fontweight="bold", 
                 fontfamily='serif', 
                 fontsize=16, 
                 color='royalblue')

pyplot(fig)

从图中可以看出,我增加了绘图的高度以减少标签中的重叠。情节变得很长,但在文本上仍然有重叠。一旦确定了解决方案,我认为将“根据需要调整相对间隙大小和图表高度以避免文本标签重叠”的逻辑嵌入到方便的绘图函数中将对库做出很大贡献。matplotlib

very long vertical timeline

python matplotlib 绘图 文本

评论


答:

1赞 Hack-R 8/8/2022 #1

我绝对对更好、更程序化的解决方案持开放态度,但现在我用相同长度的连续整数向量替换了日期,并在标签文本之外的任何地方都用它来代替实际日期。

###### Timeline#
chartdata = pd.read_csv(
    "/home/kodachi/Documents/ET/aliendb/www/app/data/Events.csv"
)

chartdata=chartdata.query('Year > 1939')
dates = pd.to_datetime(chartdata['Date_Clean_Approx'])
min_date = date(np.min(dates).year - 2, np.min(dates).month, np.min(dates).day)
max_date = date(np.max(dates).year + 2, np.max(dates).month, np.max(dates).day)

###
# fake date 
fake_d=np.c_[1:len(dates)]
###
labels = chartdata['Name']

# labels with associated dates
labels = ['{0:%d %b %Y}:\n{1}'.format(d, l) for l, d in zip (labels, dates)]

fig, ax = plt.subplots(figsize=(8, 28))#, constrained_layout=True)
_ = ax.set_xlim(-20, 20)
#_ = ax.set_ylim(min_date, max_date)
_ = ax.set_ylim(1, 96)
_ = ax.axvline(0, ymin=0.05, ymax=.985, c='deeppink', zorder=1)#ymax=0.95
#_ = ax.scatter(np.zeros(len(dates)), dates, s=120, c='palevioletred', zorder=2)
#_ = ax.scatter(np.zeros(len(dates)), dates, s=30, c='darkmagenta', zorder=3)
_ = ax.scatter(np.zeros(len(fake_d)), fake_d, s=120, c='palevioletred', zorder=2)
_ = ax.scatter(np.zeros(len(fake_d)), fake_d, s=30, c='darkmagenta', zorder=3)

#label_offsets = np.repeat(2.0, len(dates))
label_offsets = np.repeat(2.0, len(fake_d))
label_offsets[1::2] = -2.0


for i, (l, d) in enumerate(zip(labels, fake_d)): #dates
    #d = d - timedelta(days=90) 
    align = 'right'
    if i % 2 == 0:
        align = 'left'
    _ = ax.text(label_offsets[i], d, l, ha=align, fontfamily='serif', 
                fontweight='bold', color='royalblue',fontsize=12)

#stems = np.repeat(2.0, len(dates))
stems = np.repeat(2.0, len(fake_d))
stems[1::2] *= -1.0    
#x = ax.hlines(dates, 0, stems, color='darkmagenta')
x = ax.hlines(fake_d, 0, stems, color='darkmagenta')

# hide lines around chart
for spine in ["left", "top", "right", "bottom"]:
    _ = ax.spines[spine].set_visible(False)

# hide tick labels
_ = ax.set_xticks([])
_ = ax.set_yticks([])
_ = ax.set_title('UAP (UFO) Milestones, 1940 - Present', 
                 fontweight="bold", 
                 fontfamily='serif', 
                 fontsize=16, 
                 color='darkgreen')