When I run this example and create a rectangular selection if I zoom or move the plot window around the selection disappears until I deselect the move or zoom tool and click on the plot window again.
I am using %matplotlib tkinter
in an IPython notebook.
I've attempted hooking into the limit changes that occur when the window is zoomed and setting the rectangular selection to visible:
def persist_rect(newlims):
rs = toggle_selector.RS
print(rs.visible)
rs.set_visible(True)
rs.update()
current_ax.callbacks.connect('xlim_changed', persist_rect)
current_ax.callbacks.connect('ylim_changed', persist_rect)
But this doesn't seem to do anything. It doesn't even appear that toggle_selector.RS.visible
is ever set to false.
I've also been looking at the source for RectangleSelector, but I haven't seen anything enlightening there.
I've also discovered that I have this issue when I modify the extent of the selected region using RectangleSelector.extents = new_extents
. When .extents
is modified, for example with a slider widget, the selected region disappears until I click on the plot again.
All of these problems problems go away if the RectangleSelector
is initialized with useblit=False
as @ImportanceOfBeingErnest suggests, but, as they say, it's not a very performant solution.
In the source code for RectangularSelector the release method (line 2119) handles the visibility of the selector
Subclass RectangleSelector to modify the release method
Sample Code using doc example
Adding a callback for
draw_event
s:makes the
RectangleSelector
persist after zooming or panning, and is compatible withuseblit=True
.For example, using the code from the docs as a base:
Why does
mycallback
work butpersist_rect
doesn't?If you uncomment the commented-out statements above, you'll get some printed output which will look something as this:
Notice that
persist_rect
gets called beforefigure.draw
, whilemycallback
is called afterwards.figure.draw
does not draw theRectangleSelection
, but it does draw theRectangle
used for the background. Sofigure.draw
obscuresRectangleSelection
. Thuspersist_rect
momentarily displaysRectangleSelection
, but it fails to persist.mycallback
works because it is called afterfigure.draw
.If I understand correctly, the rectangle selector should stay visible throughout the process of panning or zooming. This could be achieved by not using blitting,
A side effect of this is that the plotting may become slow depending on the complexity of the plot, as without blitting, the complete plot is continuously redrawn while using the rectangle selector.