You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matplotlib散点图点上添加浮点数值标注并避免重叠

问题解决:Matplotlib散点图添加精确浮点标注并避免重叠

需求说明

使用Python和matplotlib的scatter()和errorbar()绘制图表时,需要实现以下目标:

  • 在每个散点位置显示精确的浮点数值标注
  • 避免标注之间互相重叠
  • 保存图表文件时无需额外调整,即可完整展示所有内容

原代码

import matplotlib.pyplot as plt

y = [88, 137, 134, 78, 107, 154, 108, 123, 118, 133, 125, 140, 66, 82, 28, 45, 102, 68, 4, 1, 155, 5, 124, 95, 107, 36, 127, 63, 90, 32, 64, 148, 130, 141, 73, 99, 149, 18, 3, 30, 16, 85, 149, 67, 29, 18, 153, 79, 69, 9, 124, 119, 22, 9, 10, 122, 144, 36, 5, 128, 126, 155, 34, 133, 49, 36, 60, 83, 152, 135, 40, 28, 150, 80, 150, 127, 13, 85, 56, 61, 37, 152, 25, 7, 125, 45, 59, 41, 13, 17, 56, 44, 155, 40, 14, 69, 96, 114, 128, 51, 135, 87, 132, 132, 132, 38, 83, 21, 93, 111, 43, 84, 148, 22, 24, 99, 23, 95, 72, 102, 135, 8, 27, 49, 105, 111, 43, 50, 31, 155, 28, 8, 151, 99, 28, 14, 122, 9, 147, 91, 81, 147, 28, 125]
x = [-69, -118, -115, -59, -88, -135, -89, -104, -99, -114, -106, -121, -47, -63, -9, -26, -83, -49, 15, 18, -136, 14, -105, -76, -88, -17, -108, -44, -71, -13, -45, -129, -111, -122, -54, -80, -130, 1, 16, -11, 3, -66, -130, -48, -10, 1, -134, -60, -50, 10, -105, -100, -3, 10, 9, -103, -125, -17, 14, -109, -107, -136, -15, -114, -30, -17, -41, -64, -133, -116, -21, -9, -131, -61, -131, -108, 6, -66, -37, -42, -18, -133, -6, 12, -106, -26, -40, -22, 6, 2, -37, -25, -136, -21, 5, -50, -77, -95, -109, -32, -116, -68, -113, -113, -113, -19, -64, -2, -74, -92, -24, -65, -129, -3, -5, -80, -4, -76, -53, -83, -116, 11, -8, -30, -86, -92, -24, -31, -12, -136, -9, 11, -132, -80, -9, 5, -103, 10, -128, -72, -62, -128, -9, -106]

plt.scatter(x, y, c='red')
plt.errorbar(x, y, c='red')
plt.title("EL")
plt.xlabel("X")
plt.ylabel("Y")
plt.savefig("tst.png")
plt.show()

解决方案代码

首先需要安装adjustText库来解决标注重叠问题:

pip install adjustText

修改后的代码如下:

import matplotlib.pyplot as plt
from adjustText import adjust_text

y = [88, 137, 134, 78, 107, 154, 108, 123, 118, 133, 125, 140, 66, 82, 28, 45, 102, 68, 4, 1, 155, 5, 124, 95, 107, 36, 127, 63, 90, 32, 64, 148, 130, 141, 73, 99, 149, 18, 3, 30, 16, 85, 149, 67, 29, 18, 153, 79, 69, 9, 124, 119, 22, 9, 10, 122, 144, 36, 5, 128, 126, 155, 34, 133, 49, 36, 60, 83, 152, 135, 40, 28, 150, 80, 150, 127, 13, 85, 56, 61, 37, 152, 25, 7, 125, 45, 59, 41, 13, 17, 56, 44, 155, 40, 14, 69, 96, 114, 128, 51, 135, 87, 132, 132, 132, 38, 83, 21, 93, 111, 43, 84, 148, 22, 24, 99, 23, 95, 72, 102, 135, 8, 27, 49, 105, 111, 43, 50, 31, 155, 28, 8, 151, 99, 28, 14, 122, 9, 147, 91, 81, 147, 28, 125]
x = [-69, -118, -115, -59, -88, -135, -89, -104, -99, -114, -106, -121, -47, -63, -9, -26, -83, -49, 15, 18, -136, 14, -105, -76, -88, -17, -108, -44, -71, -13, -45, -129, -111, -122, -54, -80, -130, 1, 16, -11, 3, -66, -130, -48, -10, 1, -134, -60, -50, 10, -105, -100, -3, 10, 9, -103, -125, -17, 14, -109, -107, -136, -15, -114, -30, -17, -41, -64, -133, -116, -21, -9, -131, -61, -131, -108, 6, -66, -37, -42, -18, -133, -6, 12, -106, -26, -40, -22, 6, 2, -37, -25, -136, -21, 5, -50, -77, -95, -109, -32, -116, -68, -113, -113, -113, -19, -64, -2, -74, -92, -24, -65, -129, -3, -5, -80, -4, -76, -53, -83, -116, 11, -8, -30, -86, -92, -24, -31, -12, -136, -9, 11, -132, -80, -9, 5, -103, 10, -128, -72, -62, -128, -9, -106]

# 创建绘图对象,预留足够边距防止标注被截断
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(x, y, c='red')
ax.errorbar(x, y, c='red', fmt='none')  # 避免重复绘制散点,减少视觉干扰

# 生成精确浮点标注文本,保留两位小数
texts = []
for xi, yi in zip(x, y):
    texts.append(ax.text(xi, yi, f"({xi:.2f}, {yi:.2f})", fontsize=8))

# 自动调整标注位置,避免重叠,箭头连接标注与散点
adjust_text(texts, arrowprops=dict(arrowstyle='->', color='gray', lw=0.5))

# 设置图表标签和标题
ax.set_title("EL")
ax.set_xlabel("X")
ax.set_ylabel("Y")

# 保存图表时自动调整布局,确保所有内容被包含
plt.savefig("tst.png", bbox_inches='tight', dpi=150)
plt.show()

关键优化点说明

  • 精确浮点标注:使用f"({xi:.2f}, {yi:.2f})"格式化坐标值为两位小数的浮点数,可根据需求调整小数位数
  • 避免标注重叠:借助adjustText库的adjust_text()函数,自动调整标注位置,还可通过箭头连接标注与对应散点,提升可读性
  • 完整展示内容:
    • 创建绘图对象时设置合适的figsize,预留足够空间容纳标注
    • 保存图表时添加bbox_inches='tight'参数,自动裁剪多余空白并确保所有元素被包含
    • 使用fmt='none'避免errorbar()重复绘制散点,减少视觉干扰

内容的提问来源于stack exchange,提问作者user19533273

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:50:47