防止标签文本重叠Matplotlib Python中的线图
我尝试绘制由X_TN,Y_TN列表协调为(x,y)的线,并以系列顺序标记为Seri列表。我的简化代码如下所示。但是,标签已与我的线路重叠,例如(1)和(6)。如何让我的标签一般躲避行?谢谢。
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
x_tn = [0,1,1,2,3,2,2]
y_tn = [0,0,1,2,1,1,0]
seri = [0,1,2,3,4,5,6]
plt.title("Function")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x_tn, y_tn, '-ok')
for x,y,k in zip(x_tn,y_tn,seri):
label = f"({k})"
plt.annotate(label, # this is the text
(x,y), # these are the coordinates to position the label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
#specify axis tick step sizes
plt.xticks(np.arange(min(x_tn), max(x_tn)+1, 1))
plt.yticks(np.arange(min(y_tn), max(y_tn)+1, 1))
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过此代码更改标签位置
Change label location by this piece of code