Repository: scikit-learn/scikit-learn
Pull request: MAINT/EXA replaced deprecated NavigationToolbar2TkAgg
<!--
Thanks for contributing a pull request! Please ensure you have taken a look at
the contribution guidelines: https://github.com/scikit-learn/scikit-learn/blob/master/CONTRIBUTING.md#pull-request-checklist
-->

#### Reference Issues/PRs
Fixes : #16160
<!--
Example: Fixes #1234. See also #3456.
Please use keywords (e.g., Fixes) to create link to the issues or pull requests
you resolved, so that they will automatically be closed when your pull request
is merged. See https://github.com/blog/1506-closing-issues-via-pull-requests
-->


#### Fixed 
Replaced deprecated matplotlib dependency


#### comments
Need to verify if this affects any other parts in the code


<!--
Please be aware that we are a loose team of volunteers so patience is
necessary; assistance handling other issues is very welcome. We value
all user contributions, no matter how minor they are. If we are slow to
review, either the pull request needs some benchmarking, tinkering,
convincing, etc. or more likely the reviewers are simply busy. In either
case, we ask for your understanding during the review process.
For more information, see our FAQ on this topic:
http://scikit-learn.org/dev/faq.html#why-is-my-pull-request-not-getting-any-attention.

Thanks for contributing!
-->


Problem statement:
Import Error in Libsvm GUI example
#### Describe the bug
The Libsvm GUI example (`examples/applications/svm_gui.py`) throws an import error in local installation and execution.
The matplotlib 2.x `NavigationToolbar2TkAgg` has changed its name in matplotlib 3.x.
The example should probably be modified to work with both versions.

#### Steps/Code to Reproduce
```
$ python examples/applications/svm_gui.py
```

#### Expected Results
No error is thrown.

#### Actual Results
```
Traceback (most recent call last):
  File "examples/applications/svm_gui.py", line 27, in <module>
    from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
ImportError: cannot import name 'NavigationToolbar2TkAgg' from 'matplotlib.backends.backend_tkagg' (/home/cmarmo/skldevenv/lib64/python3.7/site-packages/matplotlib/backends/backend_tkagg.py)
```

#### Versions
>>> sklearn.show_versions()

System:
    python: 3.7.5 (default, Dec 15 2019, 17:54:26)  [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
executable: /home/cmarmo/skldevenv/bin/python
   machine: Linux-5.3.16-300.fc31.x86_64-x86_64-with-fedora-31-Thirty_One

Python dependencies:
       pip: 19.3.1
setuptools: 40.8.0
   sklearn: 0.23.dev0
     numpy: 1.17.1
     scipy: 1.3.1
    Cython: 0.29.13
    pandas: 0.25.1
matplotlib: 3.1.2
    joblib: 0.13.2

Built with OpenMP: True

Hints from issue discussion:
Since we don't have strict matplotlib minimum version requirement, let's [update the import](https://stackoverflow.com/questions/50330320/what-to-use-instead-of-navigationtoolbar2tkagg) and leave a comment near the import e.g. `# requires matplotlib >= 3.0`
In this case, we also need to update our CI then. It makes probably sense to support the last major release (if they were breaking changes with previous major release)
Alternatively we can try/except the import...

I don't know what our convention is regarding minimal version of dependencies. It seems that we've been pretty conservative about it, i.e. try to be compatible with as many versions as possible.
I am not really aware of the dependencies policies and versioning, but  NavigationToolbar2TkAgg is actually deprecated, I replaced it with the the updated dependency and updated the functions and it solved the issue.

Commit message: BUG replaced deprecated NavigationToolbar2TkAgg

Patch to review:
diff --git a/examples/applications/svm_gui.py b/examples/applications/svm_gui.py
index 46b7f7369a0fe..b6e3a512c29fb 100644
--- a/examples/applications/svm_gui.py
+++ b/examples/applications/svm_gui.py
@@ -24,7 +24,7 @@
 matplotlib.use('TkAgg')
 
 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
-from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
+from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
 from matplotlib.figure import Figure
 from matplotlib.contour import ContourSet
 
@@ -144,11 +144,11 @@ def __init__(self, root, controller):
         ax.set_xlim((x_min, x_max))
         ax.set_ylim((y_min, y_max))
         canvas = FigureCanvasTkAgg(f, master=root)
-        canvas.show()
+        canvas.draw()
         canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
         canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
         canvas.mpl_connect('button_press_event', self.onclick)
-        toolbar = NavigationToolbar2TkAgg(canvas, root)
+        toolbar = NavigationToolbar2Tk(canvas, root)
         toolbar.update()
         self.controllbar = ControllBar(root, controller)
         self.f = f


Code review comments:
diff_hunk: @@ -24,7 +24,7 @@
 matplotlib.use('TkAgg')
 
 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
-from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
+from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
path: examples/applications/svm_gui.py
text: @user1:
We still want this to work with matplotlib 2, so it should be,
```py
try:
    from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
except ImportError:
    from matplotlib.backends.backend_tkagg import (
        NavigationToolbar2TkAgg as NavigationToolbar2Tk
    )
```

@author:
@user1 Just did it the way you suggested with few other changes. Hope it suits the requirements !