Search code examples
python-3.xmatplotlibmplcursors

Selectively disable mplcursors on matplotlib plots


I'm currently using mplcursors to show a label when hovering over a line on a plot, but I have a unintended consequence of it showing a unwanted label on another plot within my application.

Is there any way to enable mplcursors on 1 plot but not another?

This is what I'm using to turn on the feature mplcursors.cursor(hover=True)


Solution

  • The documentation says you can give artists or axes as inputs to mplcursors.cursor using the artists_or_axes kwarg.

    So, in your case, you should give mplcursors.cursor just the Axes instance that you would like to see cursors on, and not the other one.

    For example, something like this should work to only show cursors on ax1:

    import matplotlib.pyplot as plt
    import mplcursors
    
    fig, (ax1, ax2) = plt.subplots(2)
    
    ax1.plot(range(5))
    ax2.plot(range(5))
    
    mplcursors.cursor(artists_or_axes=ax1, hover=True)
    
    plt.show()