Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pyplot annotations

I am currently using the following code to plot a graph using python pyplot:

  plt.plot([row[2] for row in data],[row[1] for row in data], type, marker='o', label=name)  

However, instead of the default marker of 'o' I want the marker at the points to be the data in row[1]

Can someone explain how to do this?

like image 432
user1220022 Avatar asked Dec 28 '22 00:12

user1220022


1 Answers

So you want to annotate the y-values of the points along your line?

Use annotate for each point. For example:

import matplotlib.pyplot as plt

x = range(10)
y = range(10)

fig, ax = plt.subplots()

# Plot the line connecting the points
ax.plot(x, y)

# At each point, plot the y-value with a white box behind it
for xpoint, ypoint in zip(x, y):
    ax.annotate('{:.2f}'.format(ypoint), (xpoint,ypoint), ha='center', 
                va='center', bbox=dict(fc='white', ec='none'))

# Manually tweak the limits so that our labels are inside the axes...
ax.axis([min(x) - 1, max(x) + 1, min(y) - 1, max(y) + 1])
plt.show()

enter image description here

like image 98
Joe Kington Avatar answered Dec 29 '22 16:12

Joe Kington