Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control mouseover text in matplotlib

When you mouseover an image shown with imshow, you can mouseover the image to inspect its RGB values. The bottom-right corner of the matplotlib window (sharing space with the toolbar), shows the image coordinates and RGB values of the pixel being pointed at:

x = 274.99  y = 235.584  [.241, .213, .203]

However, when I mouseover a quiver plot, it only shows the x and y coords of the pointer, but not the value of the 2D vector being pointed at. Is there a way to get the vector values to show up?

I would be fine with writing a custom mouse event handler, if I only knew how to set that bit of text in the matplotlib window.

like image 676
SuperElectric Avatar asked Oct 02 '17 18:10

SuperElectric


People also ask

What is %Matplotlib inline?

Magic commands perform special functions in the IPython shell. Matplotlib Inline command is a magic command that makes the plots generated by matplotlib show into the IPython shell that we are running and not in a separate output window.

How do I show text in matplotlib?

Basic text commandsAdd an annotation, with an optional arrow, at an arbitrary location of the Axes . Add a label to the Axes 's x-axis. Add a label to the Axes 's y-axis. Add a title to the Axes .

Do You Need %Matplotlib inline?

The only reason %matplotlib inline is used is to render any matplotlib diagrams even if the plt. show() function is not called. However, even if %matplotlib inline is not used, Jupyter will still display the Matplotlib diagram as an object, with something like matplotlib. lines.

Is Plotly better than matplotlib?

Plotly has several advantages over matplotlib. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.


1 Answers

There were times when the information about the color value was not present by default. In fact I think the current version is based on some code that came up in Stackoverflow on some question about that feature.

I quickly found those two questions:

  • matplotlib values under cursor
  • Interactive pixel information of an image in Python?

The idea would be to change the function that is called when the mouse hovers the axes. This function is stored in ax.format_coord. So a possible solution is to write your custom function to return the desired output based on the input coordinates, e.g. something like

def format_coord(x, y):
    try:
        z = # get value depending on x,y, e.g. via interpolation on grid
            # I can't fill this because the kind of data is unknown here
        return "x: {}, y: {}, z: {}".format(x,y,z)

    except:
        return "x: {}, y: {}".format(x,y)

ax.format_coord = format_coord
like image 124
ImportanceOfBeingErnest Avatar answered Sep 27 '22 22:09

ImportanceOfBeingErnest