How can I show the value of each datapoint at each marker, when plotting with matplotlib.pyplot.plot() ?
Asked
Active
Viewed 3,217 times
3
-
4Perhaps you are looking for plt.annotate? http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot – unutbu Jun 19 '11 at 15:06
-
1It does the job, but there should be more elegant way. – Euphorbium Jun 19 '11 at 15:47
-
@Euphorbium: what does 'more elegant' mean: more visually appealing, or a nicer API call? What's wrong with `plt.annotate()`? – smci Jul 05 '11 at 10:51
-
1Well, it takes a loop to annotate all data points, I thought that there should be a function that does just that. – Euphorbium Jul 08 '11 at 18:31
1 Answers
1
Create a function to add the labels to a given line
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)
Example usage
x=[2,5,7,10]
y=[3.3,5.6,2.1,-.5]
line,= matplotlib.pyplot.plot(x,y)
add_labels(line)

esmit
- 1,748
- 14
- 27