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

AdjustText报错TypeError:query_pairs()不支持output_type参数的解决咨询

错误原因与解决方案

一、针对query_pairs()参数错误的修复

这个错误的核心是adjustText 1.0.4版本与scipy 1.5.3版本不兼容:adjustText 1.0+版本调用scipy.spatial.cKDTree.query_pairs()时使用了output_type参数,但该参数是scipy 1.6.0才新增的,你的scipy 1.5.3不支持该参数。

有两种直接的修复方式:

  • 降低adjustText版本到与scipy 1.5.3兼容的版本,比如你本地验证过的0.7.3.1:
    pip install adjustText==0.7.3.1 --force-reinstall
    
  • 升级scipy到1.6.0及以上版本(需确认AzureML Kernel环境允许升级,且不会破坏其他依赖项):
    pip install scipy>=1.6.0 --upgrade
    

二、其他去除文本重叠的替代方案

如果不想调整包版本,可尝试以下方案:

1. matplotlib原生微调文本位置

手动给文本添加偏移量,结合对齐方式和背景框减少视觉重叠:

import matplotlib.pyplot as plt

# 假设x、y为PCA坐标,labels为待标注的文本
fig, ax = plt.subplots()
ax.scatter(x, y)

for xi, yi, label in zip(x, y, labels):
    # 给文本添加固定偏移,同时设置半透明背景框
    ax.text(xi + 0.02, yi + 0.02, label, ha='left', va='bottom', 
            bbox=dict(facecolor='white', alpha=0.6, edgecolor='none'))

2. 拆分长标签减少占用空间

如果是长标签导致的重叠,用textwrap拆分文本:

import textwrap
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y)

for xi, yi, label in zip(x, y, labels):
    # 将长标签按指定宽度拆分
    wrapped_label = textwrap.fill(label, width=12)
    ax.text(xi, yi, wrapped_label, ha='center', va='center')

3. 基于scipy手动实现文本避碰

利用scipy的KDTree检测文本位置碰撞,手动调整重叠文本的位置:

from scipy.spatial import cKDTree
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x, y)

# 初始化所有文本并记录位置
texts = []
positions = []
for xi, yi, label in zip(x, y, labels):
    txt = ax.text(xi, yi, label)
    texts.append(txt)
    positions.append((xi, yi))

# 构建KDTree查询重叠文本对
tree = cKDTree(positions)
# r为自定义的距离阈值,根据绘图范围调整
overlap_pairs = tree.query_pairs(r=0.15)

# 调整重叠文本的位置
offset = 0.04
for idx1, idx2 in overlap_pairs:
    # 给第二个文本添加偏移
    new_x = positions[idx2][0] + offset
    new_y = positions[idx2][1] + offset
    texts[idx2].set_position((new_x, new_y))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 19:42:15