每个标记处的数据值

3 投票
1 回答
3661 浏览
提问于 2025-04-16 19:52

我该如何在使用matplotlib.pyplot.plot()绘图时,显示每个数据点在每个标记上的值呢?

1 个回答

1

创建一个函数,用来给指定的行添加标签

import matplotlib
def add_labels(line):
    x,y=line.get_data()
    labels=map(','.join,zip(map(lambda s: '%g'%s,x),map(lambda s: '%g'%s,y)))
    map(matplotlib.pyplot.text,x,y,labels)

示例用法

x=[2,5,7,10]
y=[3.3,5.6,2.1,-.5]
line,= matplotlib.pyplot.plot(x,y)
add_labels(line)

撰写回答