Skip to content
Advertisement

Draggable lines select one another in Matplotlib

I’m trying to create a class of draggable lines using matplotlib handling and picking. The aim is to set different thresholds and intervals on a graph. Here is the code:

JavaScript

The behavior is what I expected when using only 1 line (even if it notify the selection also when I release the line).

When I’m using more than one line it selects all of them at the same time!

I think I’m misunderstanding the event manager functionality, but I cannot understand why different objects, well distinguished (as I can see in the print("line selected ", event.artist)) should select themselves and another!

Advertisement

Answer

One could ask differently: How would matplotlib know which line to drag if you click on any of them? Answer: it wouldn’t, because it has three callbacks, one for each line and will execute them all.

The solution is hence to first check if the line clicked is actually the line to be moved inside the 'pick_event' callback:

JavaScript

(On a different note: You would benefit from not calling canvas.draw() so often, but instead canvas.draw_idle())

JavaScript

enter image description here

Advertisement