python - 在每个数据点处带有对齐注释的散点图

标签 python matplotlib plot label scatter-plot

我想生成一个包含数十个点的散点图,这些点可能彼此非常接近。我从问题的答案中尝试了注释的方法:

>> matplotlib scatter plot with different text at each data point

但是当点彼此足够接近时,您可以看到标签/注释重叠。是否有任何库或方法可以生成这样的带有单独注释的图,这些注释不会相互冲突,也不会与图的边界发生冲突?

import matplotlib.pyplot as plt

z = [0.15, 0.3, 0.45, 0.46, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, "393(2)", 203, 123]

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

for i, txt in enumerate( n ):
    ax.annotate( txt, ( z[i] + .01, y[i] + .01 ) )

plt.show()
Scatter plot with colliding annotations

最佳答案

我编写了一个库 adjustText 来做这件事。 https://github.com/Phlya/adjustText

z = [0.15, 0.3, 0.45, 0.46, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, "393(2)", 203, 123]

fig, ax = plt.subplots()
ax.scatter( z, y )
texts = []
for i, txt in enumerate( n ):
    texts.append(ax.text(z[i], y[i], txt))
adjust_text(texts)
plt.show()

enter image description here

关于python - 在每个数据点处带有对齐注释的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29233683/

相关文章:

python - 在深度优先搜索中跟踪和返回路径

python - 对字符串进行 URL 编码

python - 子图中的子图 - 两个 8x8 子图

Python:使用自动 Y 缩放绘制烛台

r - 在 ggplot 或 base R 中使用圆形带和标签进行绘图?

python - 3.3.5 更新后打开 .py 文件

python - 使用 opencv 和 ffmpeg 制作视频。如何找到合适的颜色格式?

python - pandas 散点图与一天中时间的关系?

python - 以 3D 方式绘制图像导致交互非常缓慢

python - 如何在matplotlib中突出显示多条曲线中最低的曲线(包络线)