[英]Python matplotlib anotate overlaping points
我有一种情况,我需要 plot 一个它们可以重叠的点列表。 问题是点 label 是重叠的,我需要确定哪个点在哪里。
有示例代码:
import matplotlib.pyplot as plt
Nu = [6.0155, 12.031, 12.031, 12.031, 12.031]
Ra = [40.22, 40.66, 40.66, 40.66, 40.66]
P = ["P_1", "P_2", "P_3", "P_4", "P_5"]
fig, ax = plt.subplots()
plt.scatter(Ra, Nu)
for i, txt in enumerate(P):
plt.annotate(txt, (Ra[i], Nu[i]))
得到这个结果:
其中 5 个点和注释在右上角重叠。
我想你已经使用那些相同的坐标来过度夸大你的例子。 如果是这种情况,有一个不错的 package 可以帮助您适应 matplotlib 中的注释。
package https://pypi.org/project/adjustText/
文档https://adjusttext.readthedocs.io/en/latest/
我知道使用 plt.text 而不是 plt.annotate 效果很好
import matplotlib.pyplot as plt
from adjustText import adjust_text
Nu = [6.0155, 12.031, 12.031, 12.031, 12.031]
Ra = [40.22, 40.66, 40.66, 40.66, 40.66]
P = ["P_1", "P_2", "P_3", "P_4", "P_5"]
fig, ax = plt.subplots()
plt.scatter(Ra, Nu)
texts = [plt.text(Ra[i], Nu[i], txt,size=16) for i, txt in enumerate(P)]
adjust_text(texts, arrowprops=dict(arrowstyle="->", color='r'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.