mpldatacursor icon indicating copy to clipboard operation
mpldatacursor copied to clipboard

Can't get mpldatacursor to work when used inside Gtk3 app

Open simonvanderveldt opened this issue 8 years ago • 2 comments

Hi Joe, thanks for this library, it looks exactly like the thing I was looking for. Unfortunately I can't get it to work in my Gtk3 app. Initially it would fail with the following error when passing the lines as artists:

Traceback (most recent call last):
  File "pe.py", line 120, in <module>
    main()
  File "pe.py", line 82, in main
    datacursor(artists=lines)
  File "/home/simon/src/simonvanderveldt/personal-expenses/.venv/lib64/python3.4/site-packages/mpldatacursor/convenience.py", line 168, in datacursor
    return DataCursor(artists, **kwargs)
  File "/home/simon/src/simonvanderveldt/personal-expenses/.venv/lib64/python3.4/site-packages/mpldatacursor/datacursor.py", line 204, in __init__
    fig.canvas.mpl_connect('key_press_event', self._on_keypress)
AttributeError: 'NoneType' object has no attribute 'mpl_connect'

But I've managed to fix that by explicitly creating the canvas.

When calling datacursor() without arguments it seemed like nothing happened. I didn't get an error, but it also didn't work. Now that I've fixed the above error when passing the artists to datacursor(artists=ax.lines) nothing is happening as well, no annotation is shown when I click on a point or line.

Any idea how to trace where it's going wrong? Maybe it's because I'm not using pyplot/ no show() is called?

simonvanderveldt avatar Dec 11 '16 17:12 simonvanderveldt

First off, it looks like you've found a bug. What you're doing should have worked, but mpldatacursor isn't checking for figures correctly when pyplot isn't being used and it's passed a list of artists.

However, there should be an easy workaround. Try specifying the axes the lines are plotted on instead of the artists themselves. E.g. datacursor(axes=[ax])

Hope that helps!

joferkington avatar Dec 11 '16 22:12 joferkington

First off, it looks like you've found a bug. What you're doing should have worked, but mpldatacursor isn't checking for figures correctly when pyplot isn't being used and it's passed a list of artists.

Ah, good to know it's not me ;) Is this the line that's the cause of this?

However, there should be an easy workaround. Try specifying the axes the lines are plotted on instead of the artists themselves. E.g. datacursor(axes=[ax])

Hope that helps!

It indeed helped, thanks a lot! It works now :) I do get another issue though, that happens when the annotation is shown. See Traceback below. Shall I create a new issue for this?

Traceback (most recent call last):
  File "/usr/lib64/python3.4/site-packages/matplotlib/backends/backend_gtk3cairo.py", line 45, in on_draw_event
    self._render_figure(w, h)
  File "/usr/lib64/python3.4/site-packages/matplotlib/backends/backend_gtk3cairo.py", line 34, in _render_figure
    self.figure.draw (self._renderer)
  File "/usr/lib64/python3.4/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python3.4/site-packages/matplotlib/figure.py", line 1085, in draw
    func(*args)
  File "/usr/lib64/python3.4/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python3.4/site-packages/matplotlib/text.py", line 2086, in draw
    Text.draw(self, renderer)
  File "/usr/lib64/python3.4/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python3.4/site-packages/matplotlib/text.py", line 642, in draw
    ismath=ismath, mtext=mtext)
  File "/usr/lib64/python3.4/site-packages/matplotlib/backends/backend_cairo.py", line 194, in draw_text
    self._draw_mathtext(gc, x, y, s, prop, angle)
  File "/usr/lib64/python3.4/site-packages/matplotlib/backends/backend_cairo.py", line 247, in _draw_mathtext
    ctx.show_text(s)
TypeError: Can't convert 'bytes' object to str implicitly

simonvanderveldt avatar Dec 12 '16 19:12 simonvanderveldt