Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: How to pick up shift click on figure?

I have a matplotlib and I have created a button_press_event like this:

self.fig.canvas.mpl_connect('button_press_event', self.onClick)

def onClick(self, event)
    if event.button == 1:
        # draw some artists on left click

    elif event.button == 2:
        # draw a vertical line on the mouse x location on wheel click

    elif event.button == 3:
        # clear artists on right click

Now is it possible to modify the wheel click handler to something like this

    elif event.button == 2 or (event.button == 1 and event.key == "shift"):
        # draw a vertical line on the mouse x location 
        # on wheel click or on shift+left click 
        # (alternative way if there is no wheel for example)

It seems that button_press_event doesn't support keys and key_press_event doesn't support mouse button clicks, but I'm not sure.

Is there a way?

like image 678
NotNone Avatar asked Aug 09 '13 06:08

NotNone


People also ask

How do I make matplotlib zoomable?

Press the right mouse button to zoom, dragging it to a new position. The x axis will be zoomed in proportionately to the rightward movement and zoomed out proportionately to the leftward movement. The same is true for the y axis and up/down motions.

What is matplotlib interactive mode?

Matplotlib can be used in an interactive or non-interactive modes. In the interactive mode, the graph display gets updated after each statement. In the non-interactive mode, the graph does not get displayed until explicitly asked to do so.

How do I remove the margins in matplotlib?

The python plotting library matplotlib will by default add margins to any plot that it generates. They can be reduced to a certain degree through some options of savefig() , namely bbox_inches='tight' and pad_inches=0 .

What is an artist in matplotlib?

and the matplotlib. artist. Artist is the object that knows how to use a renderer to paint onto the canvas.


1 Answers

You can also bind a key press and key release events and do something like:

self.fig.canvas.mpl_connect('key_press_event', self.on_key_press)
self.fig.canvas.mpl_connect('key_release_event', self.on_key_release)

...

def on_key_press(self, event):
   if event.key == 'shift':
       self.shift_is_held = True

def on_key_release(self, event):
   if event.key == 'shift':
       self.shift_is_held = False

Then you can check in your onClick function if self.shift_is_held.

if event.button == 3:
    if self.shift_is_held:
        do_something()
    else:
        do_something_else()
like image 60
Viktor Kerkez Avatar answered Nov 14 '22 23:11

Viktor Kerkez