简体   繁体   English

Matplotlib:鼠标悬停时的标签点

[英]Matplotlib: Label points on mouseover

I have a scatter plot with several thousand points. 我有几千点的散点图。 This post tells me how to label them: 这篇文章告诉我如何标记它们:

Matplotlib: How to put individual tags for a scatter plot Matplotlib:如何为散点图放置单个标签

But that will look like a disaster with so many points. 但这看起来像是一场有如此多点的灾难。 What I would like instead is to have a "tool tip" type label that pops up when you mouseover a point. 我想要的是有一个“工具提示”类型标签,当你鼠标悬停一个点时弹出。 Is that possible using matplotlib? 这可能使用matplotlib吗?

Once you get the coords of the point you can show them or any object-linked info in a textctrl in the toolbar. 获得该点的坐标后,您可以在工具栏的文本控件中显示它们或任何与对象相关的信息。 For this you have to instantiate a toolbar (NavigationToolbar2Wx()) in your canvas and add the textcontrol there. 为此,您必须在画布中实例化工具栏(NavigationToolbar2Wx())并在其中添加textcontrol。 This is not as nice as a popup but it does the job. 这不像弹出窗口那样好,但它可以完成这项工作。

Here you have an example of customizing your toolbar (only showing the x coordinate in the txtctrl): 这里有一个自定义工具栏的示例(仅显示txtctrl中的x坐标):

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
"""
jvisor_spectrum_panel (visor_07)
25 julio 2010
"""
#
import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
#
#
class SpectrumPanel(wx.Panel):
    def __init__(self, parent, xlabel='m/z', ylabel='Intensity'):
        wx.Panel.__init__(self, parent)
        #
        self.parent = parent
        self.xlabel = xlabel
        self.ylabel = ylabel
        self.SetBackgroundColour("white")
        #
        self.figure = Figure()
        self.canvas = FigureCanvas(self, -1, self.figure)
        #
        self.add_toolbar()
        #
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP| wx.GROW| wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.LEFT)
        self.canvas.mpl_connect('motion_notify_event', self.on_motion)
        self.SetSizer(sizer)
        self.Fit()
        self.clean()
    #
    def add_toolbar(self):
        ""
        self.toolbar = NavigationToolbar2Wx(self.canvas)

        mass_txt = wx.StaticText(self.toolbar, label='m/z', pos=(230, 7),
                                                             size=(25, 17))
        mass_txt.SetBackgroundColour("light gray")
        self.mass = wx.TextCtrl(self.toolbar, pos=(260,4), size=(50, 22),
                                                           style=wx.TE_READONLY)
        #
        self.toolbar.SetToolBitmapSize(wx.Size(24, 25))
        self.toolbar.SetMinSize((1500, 31))
        self.toolbar.Realize()
        self.toolbar.Update()
    #
    def clean(self):
        ""
        self.figure.clear()
        self.axes = self.figure.add_subplot(111)
    #
    def dibuja(self):
        "dibuja el canvas"
        self.axes.set_xlabel(self.xlabel) 
        self.axes.set_ylabel(self.ylabel)
        self.canvas.draw()
    #
    def on_motion(self, evt):
        if evt.inaxes:
            xpos = evt.xdata
            self.mass.SetValue(' %0.1f' % (xpos))


if __name__ == '__main__':
    ""
    class TestFrame(wx.Frame):
        def __init__(self, *args, **kargs):
            wx.Frame.__init__(self, *args, **kargs)
            self.panel = SpectrumPanel(self)
            self.Fit()
    #        
    app = wx.PySimpleApp()
    fr = TestFrame(None)
    fr.Show()
    app.MainLoop()

And here you can see the new control in the toolbar: 在这里,您可以在工具栏中看到新控件:

在此输入图像描述

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

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