简体   繁体   English

plt.plot 在 python 中,注释

[英]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?如何获得 -0.1 和 0.1 之间的随机数以站在 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() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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