Adding a Cursor in WX

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will guide you through the process of adding a cursor in WX to report data coordinates. We will be using Matplotlib, a plotting library for Python that provides tools to create a variety of plots, charts, and graphs.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-49035{{"`Adding a Cursor in WX`"}} matplotlib/line_plots -.-> lab-49035{{"`Adding a Cursor in WX`"}} matplotlib/event_handling -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/conditional_statements -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/tuples -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/sets -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/function_definition -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/classes_objects -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/constructor -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/polymorphism -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/encapsulation -.-> lab-49035{{"`Adding a Cursor in WX`"}} python/build_in_functions -.-> lab-49035{{"`Adding a Cursor in WX`"}} end

Create a Canvas Frame

First, we will create a canvas frame that will hold the Matplotlib plot. We will add a sinusoidal plot to demonstrate the cursor functionality.

class CanvasFrame(wx.Frame):
    def __init__(self, ):
        super().__init__(None, -1, 'CanvasFrame', size=(550, 350))

        ## Create a Figure and add a subplot
        self.figure = Figure()
        self.axes = self.figure.add_subplot()
        t = np.arange(0.0, 3.0, 0.01)
        s = np.sin(2*np.pi*t)

        ## Plot the sinusoidal curve
        self.axes.plot(t, s)
        self.axes.set_xlabel('t')
        self.axes.set_ylabel('sin(t)')

        ## Create a FigureCanvas to display the plot
        self.figure_canvas = FigureCanvas(self, -1, self.figure)

        ## Bind the motion_notify_event to update the status bar
        self.figure_canvas.mpl_connect(
            'motion_notify_event', self.UpdateStatusBar)

        ## Bind the enter_window event to change the cursor
        self.figure_canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)

        ## Create a sizer and add the FigureCanvas to it
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.figure_canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

        ## Create a status bar to report the cursor location
        self.statusBar = wx.StatusBar(self, -1)
        self.SetStatusBar(self.statusBar)

        ## Create a toolbar to navigate the plot
        self.toolbar = NavigationToolbar2Wx(self.figure_canvas)
        self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.toolbar.Show()

Change the Cursor

Next, we will define a method to change the cursor when it enters the canvas frame. In this case, we will change the cursor to a bullseye.

def ChangeCursor(self, event):
    self.figure_canvas.SetCursor(wx.Cursor(wx.CURSOR_BULLSEYE))

Update the Status Bar

Finally, we will define a method to update the status bar with the cursor location whenever the mouse moves over the plot.

def UpdateStatusBar(self, event):
    if event.inaxes:
        self.statusBar.SetStatusText(f"x={event.xdata}  y={event.ydata}")

Summary

Congratulations! You have successfully added a cursor in WX to report data coordinates using Matplotlib. By following the steps outlined in this tutorial, you can easily customize the cursor to fit your needs.

Other Matplotlib Tutorials you may like