问题
I want to label every dot I plot in python, and I didn't find a proper way to do it.
Assuming I have two lists of n elements called a and b, I print them this way :
plt.figure()
plt.grid()
plt.plot(a , b , 'bo')
plt.show()
I want to label every point with "Variable k" with k ranging from 1 to n obviously.
Thanks for your time
回答1:
You can use the label plot parameter
x = np.random.random(3)
y = np.random.random(3)
z = np.arange(3)
colors = ["red", "yellow", "blue"]
c = ["ro", "yo", "bo"]
for i in z:
plt.plot(x[i], y[i], c[i], label=colors[i] + ' ' + str(i))
plt.legend()
回答2:
Here is the best way of doing it I found :
plt.figure()
plt.scatter(a,b)
labels = ['Variable {0}'.format(i+1) for i in range(n)]
for i in range (0,n):
xy=(a[i],b[i])
plt.annotate(labels[i],xy)
plt.plot()
More infos : Matplotlib: How to put individual tags for a scatter plot
来源:https://stackoverflow.com/questions/41349942/label-data-when-doing-a-scatter-plot-in-python