9

When I run the following script:

import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.figure
import matplotlib.backends.backend_tkagg
import numpy as np

def on_key_event(event, canvas, toolbar):
    matplotlib.backend_bases.key_press_handler(event, canvas, toolbar)

matplotlib.use('TkAgg')
root = tk.Tk()
root.wm_title('Test window')
fig = matplotlib.figure.Figure(figsize=(9.333, 7), dpi=100)
a = fig.add_subplot(111)
axes = fig.gca()
x = np.linspace(0, 2*np.pi, 100)
axes.plot(x, np.sin(x), marker='.')
axes.set_title('sin(x)')
axes.grid()
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(fill=tk.X, expand=1)
canvas.mpl_connect(
    'key_press_event',
    lambda event: on_key_event(event, canvas, toolbar)
)
toolbar = matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg(
    canvas, root
)
toolbar.update()
root.bind('<Control-w>', lambda event: root.destroy())
tk.mainloop()

I get a warning:

MatplotlibDeprecationWarning: The NavigationToolbar2TkAgg class was
deprecated in version 2.2.

Why is the NavigationToolbar2TkAg deprecated and what should I use instead?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174

2 Answers2

29

What to use instead?

Matplotlib now wants you to use

NavigationToolbar2Tk

instead of NavigationToolbar2TkAgg.

Why is it deprecated?

The Navigation toolbar is independent of the renderer. E.g. both the Agg renderer as well as the cairo renderer can use the same navigation toolbar. Hence it makes sense to provide it under a name that does not have the renderer's name ("Agg") in it.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 1
    Ideally the documentation for NavigationToolbar2TkAgg in v2.2 and above would direct you to the class to use in replace of it instead of just telling you that it's deprecated. – Daniel Kocevski Sep 19 '18 at 20:48
  • @DanielKocevski Looks like the NavigationToolbar2TkAgg isn't actually documented anywhere. Did you find a link to it somewhere? – ImportanceOfBeingErnest Sep 19 '18 at 21:01
  • 1
    Yes, you can find a reference to it in the 2.2.3 documentation [here](https://matplotlib.org/2.2.3/api/backend_tkagg_api.html?highlight=navigationtoolbar2tkagg#matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg). I don't think it appears in the 3.0 docs, as far as I can tell. More generally, though, it would be super useful for the matplotlib documentation to reference the new class when an old one is deprecated. – Daniel Kocevski Sep 19 '18 at 21:43
  • @DanielKocevski There is an `alternative` field which can be used to point to alternatives for deprecated functions/classes/etc. (see e.g. [here](https://matplotlib.org/2.2.3/api/mlab_api.html#matplotlib.mlab.griddata)) This has unfortunately not been used in this case. – ImportanceOfBeingErnest Sep 19 '18 at 22:07
0

I beleive this means it was stopped being updated therefore may be unstable. I use it in my current project, the error always shows but the toolbar works fine.