如何使用matplotlib在单击和悬停时显示散点值? IAM是matplotlib的新手

如何解决如何使用matplotlib在单击和悬停时显示散点值? IAM是matplotlib的新手

这是我想做的事情:

这是我用于颜色参考的数据
#以浮点格式创建cqi列列表

li=[]

for x in data.lte_cqi_cw0_1:
    li.append(float(x))

我从导入的csv创建了x和y轴数据的列表

#create x and y axis data
x = []
y = []
for a in data.positioning_lon:
    x.append(float(a))
for a in data.positioning_lat:
    y.append(float(a))
 

设置颜色条件

c = np.where(data.lte_cqi_cw0_1 < 7,'r','g')

#plot the map
fig,ax=plt.subplots()
scatter=ax.scatter(x,y,c=c)

我正在使用此方法显示每个点的值。我想使用on_click事件和悬停事件来显示基于鼠标移动的值。

#display each point value        
for i,value in enumerate(li):
    
    annot = ax.annotate(value,(x[i],y[i]))
    annot.set_visible(False)


#set legend
red = mpatches.Patch(color='red',label="0 to 7 - " + str(low_per) + "%")
green = mpatches.Patch(color='green',label="7 to 15 - " + str(high_per) + "%")
plt.legend(handles=[red,green ])

plt.show()

解决方法

好的,我找到了解决方法。 iam使用以下annote方法。

annot = ax.annotate("",xy=(0,0),xytext=(20,20),textcoords="offset points",bbox=dict(boxstyle="round",fc="w"),arrowprops=dict(arrowstyle="- 
>"))
annot.set_visible(False)

然后定义一个根据索引i更新注释值的函数,它将从click事件中接收。

def update_annot(ind):
    pos = scatter.get_offsets()[ind]
    annot.xy = pos
    text = " CQI {}".format(" ".join([str(round(li[ind],2))]))
    annot.set_text(text)
    annot.get_bbox_patch().set_facecolor((c[ind]))
    annot.get_bbox_patch().set_alpha(0.4)
    return text

以下onpick函数可以很好地实现我的目标。

def onpick(event):
    #vis = annot.get_visible()
    #annot.set_visible(True)
    global ind
    ind = event.ind[-1]
    print("first value ind"+str(ind))

    update_annot(ind)
    annot.set_visible(True)
    fig.canvas.draw_idle()
    #print (len(ind))
    #print('onpick points:',update_annot(ind))

我也发现了如下按键事件。

def press(event):
    key = event.key
    sys.stdout.flush()
    global ind
    if key == 'left':

        ind += 1
        print("2nd value ind" + str(ind))
        update_annot(ind)
        annot.set_visible(True)
        fig.canvas.draw_idle()
    elif key == 'right':

        ind -= 1
        print("3rd value ind" + str(ind))
        update_annot(ind)
        annot.set_visible(True)
        fig.canvas.draw_idle()

然后将onpick和keypress函数都连接到matplotlib的内部函数。

fig.canvas.mpl_connect('pick_event',onpick)
fig.canvas.mpl_connect('key_press_event',press)

完整代码可以找到here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?