简体   繁体   中英

plt.plot in python, annotate

How can i get a random number between -0.1 and 0.1 to stand next to the data in the plot? This is what I've tried so far, but it is not working...

grades = np.array([[-3,3,7],
                   [7,0,12],
                   [2,4,4],
                   [7,12,10]])

tasks = np.arange(1,grades.shape[1]+1)
n = np.random.uniform(-0.1,0.1,len(grades))

plt.figure()
for i in range(len(grades)):
    plt.plot(tasks,grades[i,:],'b.')

    for i, txt in enumerate(n):
        plt.annotate(txt, (tasks[i],grades[i,:]))

plt.show()

Try this:

grades = np.array([[-3,3,7],
                   [7,0,12],
                   [2,4,4],
                   [7,12,10]])

tasks = np.arange(1,grades.shape[1]+1)
n = np.random.uniform(-0.1,0.1,len(grades))

for i in range(len(grades)):
    plt.plot(tasks,grades[i,:],'g^')
    for j, txt in enumerate(n):
        plt.annotate(txt, (tasks[j-1],grades[i-1,j-1]))
plt.show()


#Edit:
#Can u try this For unique labelling: 

import matplotlib.pyplot as plt
grades = np.array([[-3,3,7],
                   [7,0,12],
                   [2,4,4],
                   [7,12,10]])

tasks = np.arange(1,grades.shape[1]+1)
n = np.random.uniform(-0.1,0.1,len(grades))
tmp_list = []
for i in range(len(grades)):
    plt.plot(tasks,grades[i,:],'g^')
    for j, txt in enumerate(n):        
        if (tasks[j-1],grades[i-1,j-1]) not in tmp_list:
            plt.annotate(txt, (tasks[j-1],grades[i-1,j-1]))
        tmp_list.append((tasks[j-1],grades[i-1,j-1]),)
plt.show() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM