Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpldatacursor doesn't work when matplotlib plot embedded in tkinter canvas #100

Open
juliaolkhovyk opened this issue Sep 27, 2020 · 3 comments

Comments

@juliaolkhovyk
Copy link

juliaolkhovyk commented Sep 27, 2020

Here is my code

import matplotlib
import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
matplotlib.use('Qt5Agg')
root = tkinter.Tk()
fig, ax = plt.subplots(1, 1)

X, Y = [], []

X.append(0)
Y.append(0)
count = 1
for i in range(1, 10):
    X.append(X[i-1] + 2)
    for j in range(1, 10):
        Y.append(Y[j-1] + 2)
        ax.scatter(X[i], Y[j], color='blue', s=10, label='{}'.format(count))
        count += 1


ax.grid(which='major', color='#CCCCCC', linestyle='--')
ax.grid(which='minor', color='#CCCCCC', linestyle=':')
datacursor(formatter='{label}'.format, display='multiple')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
root.mainloop()

the hint
Безымянный
the hint is supposed to be shown after clicking on any blue dot on the plot, even though when I used without tkinter library it worked perfectly

@joferkington
Copy link
Owner

@Yulia444 - For the case where you're constructing the figure and embedding in a lower-level widget, mpldatacursor can't automatically find which Matplotlib artists and figures it should use. That's because things are managed in a different way than they would be if you didn't embed the plot, and there's no global index of figures/etc for mpldatacursor to look at.

Therefore, you should explicitly pass in the artists that you'd like mpldatacursor to work with. E.g. https://github.com/joferkington/mpldatacursor/blob/master/examples/basic.py

The first argument to datacursor is always an artist or a sequence of artists. Artists are what matplotlib plotting routines return. In your case above, you'd likely want to plot everything with a single scatter call, but you could also append the artist that ax.scatter returns to a list and then pass the list into datacursor. (Also, for this case, you might have a look at point_labels, e.g. https://github.com/joferkington/mpldatacursor/blob/master/examples/labeled_points_example.py )

@juliaolkhovyk
Copy link
Author

I launched the second example, clicked on the point and then the error occured:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\matplotlib\backends_backend_tk.py", line 293, in button_press_event
FigureCanvasBase.button_press_event(
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\matplotlib\backend_bases.py", line 1854, in button_press_event
self.callbacks.process(s, mouseevent)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\matplotlib\cbook_init_.py", line 229, in process
self.exception_handler(exc)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\matplotlib\cbook_init_.py", line 81, in exception_printer
raise exc
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\matplotlib\cbook_init
.py", line 224, in process
func(*args, **kwargs)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 718, in _select
self(new_event)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 235, in call
self._show_annotation_box(event)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 275, in _show_annotation_box
self.update(event, annotation)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 575, in update
annotation.set_text(self.formatter(**info))
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 348, in _formatter
x = self._format_coord(x, ax.xaxis)
File "C:\Users\User\Desktop\Новая папка (2)\env\lib\site-packages\mpldatacursor\datacursor.py", line 413, in _format_coord
return formatter.pprint_val(x)
AttributeError: 'ScalarFormatter' object has no attribute 'pprint_val'

@joferkington
Copy link
Owner

joferkington commented Sep 28, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants