Search code examples
pythonmatplotlibwarningsaxes

Pyplot warning when evaluating expression from textbox widget


If I have a pyplot figure with more than one "axes" (as they call them) and there is a textbox in one of them, when writing some special sequences of characters (e.g. *2) I get a warning that states the following:

MatplotlibDeprecationWarning: Toggling axes navigation from the keyboard is deprecated since 3.3 and will be removed two minor releases later.
  return self.func(*args)

Note that this doesn't seem to happen if I only have one single axes.

I'd need to use such a textbox to intert a function that will be evaluated, so I need to work with * and ** perhaps. What is causing this warning?

Here's a minimal example to recreate the scenario:

import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox

fig, (ax1, ax2) = plt.subplots(1, 2)
tb1 = TextBox(ax1, 'Textbox: ')
ax2.plot([1,2,3,4,5])
plt.show()

Solution

  • It seems you can unbind default key bindings in matplotlib:

    import matplotlib.pyplot as plt
    from matplotlib.widgets import TextBox
    
    fig, (ax1, ax2) = plt.subplots(1, 2)
    fig.canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)
    tb1 = TextBox(ax1, 'Textbox: ')
    ax2.plot([1,2,3,4,5])
    plt.show()
    

    More info here - you can apparently also specify which binding to ignore.

    Another way would be to just suppress this warning:

    import warnings
    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.widgets import TextBox
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)
        fig, (ax1, ax2) = plt.subplots(1, 2)
        tb1 = TextBox(ax1, 'Textbox: ')
        ax2.plot([1,2,3,4,5])
        plt.show()