Enter Figure on Macs

All,

Any Mac users out there? This script from the User Guide does not seem to recognize entering or leaving a figure. Any thoughts?

# enterleave.py

import matplotlib.pyplot as plt

def enter_axes(event):
    print 'enter_axes', event.inaxes
    event.inaxes.patch.set_facecolor('yellow')
    event.canvas.draw()

def leave_axes(event):
    print 'leave_axes', event.inaxes
    event.inaxes.patch.set_facecolor('white')
    event.canvas.draw()

def enter_figure(event):
    print 'enter_figure', event.canvas.figure
    event.canvas.figure.patch.set_facecolor('red')
    event.canvas.draw()

def leave_figure(event):
    print 'leave_figure', event.canvas.figure
    event.canvas.figure.patch.set_facecolor('grey')
    event.canvas.draw()

fig1 = plt.figure()
fig1.suptitle('mouse hover over figure or axes to trigger events')
ax1 = fig1.add_subplot(211)
ax2 = fig1.add_subplot(212)

fig1.canvas.mpl_connect('figure_enter_event', enter_figure)
fig1.canvas.mpl_connect('figure_leave_event', leave_figure)
fig1.canvas.mpl_connect('axes_enter_event', enter_axes)
fig1.canvas.mpl_connect('axes_leave_event', leave_axes)

fig2 = plt.figure()
fig2.suptitle('mouse hover over figure or axes to trigger events')
ax1 = fig2.add_subplot(211)
ax2 = fig2.add_subplot(212)

fig2.canvas.mpl_connect('figure_enter_event', enter_figure)
fig2.canvas.mpl_connect('figure_leave_event', leave_figure)
fig2.canvas.mpl_connect('axes_enter_event', enter_axes)
fig2.canvas.mpl_connect('axes_leave_event', leave_axes)

plt.show()

David

My Mac is currently dead, but I developed these events while I was a
mac user and so am pretty sure they worked. It is more likely a
specific backend problem than a Mac vs non-Mac problem (eg it may be
specific to the macosx backend but not a problem for macs in general)

  http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend

Could you try running the script with the "use" directive at the top
of your script (before any other mpl code) for different backends, eg
tkagg vs macosx vs wxagg

  import matplotlib
  matplotlib.use('tkagg') # and also try macosx and wxagg

so we can see exactly where the problem is arising. Also, run your
script with --verbose-helpful and report the debugging output.
Looking at the code, it appears the enter_notify_event and
leave_notify_event are only defined for wx, qt and gtk currently, and
so need to be implemented for tk and macosx.

JDH

···

On Sun, Feb 14, 2010 at 6:53 PM, David Arnold <dwarnold45@...2108...> wrote:

All,

Any Mac users out there? This script from the User Guide does not seem to recognize entering or leaving a figure. Any thoughts?

John,

Only the wxagg worked. Here is the output:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from /Users/darnold/.matplotlib/fontList.cache
backend WXAgg version 2.8.10.1
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium to Bitstream Vera Sans (/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf) with score of 0.000000
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=12.0 to Bitstream Vera Sans (/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf) with score of 0.000000
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
leave_figure Figure(640x480)

Did not work with macosx:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from /Users/darnold/.matplotlib/fontList.cache
backend MacOSX version unknown
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)

Did not work with tkagg:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from /Users/darnold/.matplotlib/fontList.cache
backend TkAgg version 8.4
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium to Bitstream Vera Sans (/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf) with score of 0.000000
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=12.0 to Bitstream Vera Sans (/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf) with score of 0.000000
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)

···

On Feb 14, 2010, at 7:50 PM, John Hunter wrote:

On Sun, Feb 14, 2010 at 6:53 PM, David Arnold <dwarnold45@...2108...> wrote:

All,

Any Mac users out there? This script from the User Guide does not seem to recognize entering or leaving a figure. Any thoughts?

My Mac is currently dead, but I developed these events while I was a
mac user and so am pretty sure they worked. It is more likely a
specific backend problem than a Mac vs non-Mac problem (eg it may be
specific to the macosx backend but not a problem for macs in general)

http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend

Could you try running the script with the "use" directive at the top
of your script (before any other mpl code) for different backends, eg
tkagg vs macosx vs wxagg

import matplotlib
matplotlib.use('tkagg') # and also try macosx and wxagg

so we can see exactly where the problem is arising. Also, run your
script with --verbose-helpful and report the debugging output.
Looking at the code, it appears the enter_notify_event and
leave_notify_event are only defined for wx, qt and gtk currently, and
so need to be implemented for tk and macosx.

JDH

I almost have a solution for this for the Mac OS X backend. I am stuck though at what I should pass to enter_notify_event and leave_notify_event for the guiEvent:

    def leave_notify_event(self, guiEvent=None):
        """
        Backend derived classes should call this function when leaving
        canvas

        *guiEvent*
            the native UI event that generated the mpl event

        """

What are the requirements for guiEvent? If I call leave_notify_event without guiEvent, so guiEvent = None, then the example gives me the following error:

enter_figure Figure(640x480)
leave_figure
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backend_bases.py", line 1416, in leave_notify_event
    self.callbacks.process('figure_leave_event', LocationEvent.lastevent)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/cbook.py", line 169, in process
    func(*args, **kwargs)
  File "test.py", line 23, in leave_figure
    print 'leave_figure', event.canvas.figure
AttributeError: 'NoneType' object has no attribute 'canvas'

So I guess I should pass some type of event object to enter|leave_notify_event.

--Michiel.

···

--- On Sun, 2/14/10, David Arnold <dwarnold45@...2108...> wrote:

From: David Arnold <dwarnold45@...2108...>
Subject: Re: [Matplotlib-users] Enter Figure on Macs
To: "John Hunter" <jdh2358@...287...>
Cc: "Michiel de Hoon" <mjldehoon@...9...>, matplotlib-users@...712...orge.net
Date: Sunday, February 14, 2010, 11:50 PM
John,

Only the wxagg worked. Here is the output:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path
/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from
/Users/darnold/.matplotlib/fontList.cache
backend WXAgg version 2.8.10.1
findfont: Matching
:family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium
to Bitstream Vera Sans
(/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf)
with score of 0.000000
findfont: Matching
:family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=12.0
to Bitstream Vera Sans
(/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf)
with score of 0.000000
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
leave_figure Figure(640x480)
enter_figure Figure(640x480)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
leave_figure Figure(640x480)

Did not work with macosx:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path
/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from
/Users/darnold/.matplotlib/fontList.cache
backend MacOSX version unknown
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)

Did not work with tkagg:

$HOME=/Users/darnold
CONFIGDIR=/Users/darnold/.matplotlib
matplotlib data path
/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/darnold/.matplotlib/matplotlibrc
matplotlib version 0.99.1.1
verbose.level helpful
interactive is False
units is False
platform is darwin
Using fontManager instance from
/Users/darnold/.matplotlib/fontList.cache
backend TkAgg version 8.4
findfont: Matching
:family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium
to Bitstream Vera Sans
(/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf)
with score of 0.000000
findfont: Matching
:family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=12.0
to Bitstream Vera Sans
(/Library/Frameworks/Python.framework/Versions/4.3.0/lib/python2.5/site-packages/matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg/matplotlib/mpl-data/fonts/ttf/Vera.ttf)
with score of 0.000000
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)
enter_axes Axes(0.125,0.536364;0.775x0.363636)
leave_axes Axes(0.125,0.536364;0.775x0.363636)
enter_axes Axes(0.125,0.1;0.775x0.363636)
leave_axes Axes(0.125,0.1;0.775x0.363636)

On Feb 14, 2010, at 7:50 PM, John Hunter wrote:

> On Sun, Feb 14, 2010 at 6:53 PM, David Arnold <dwarnold45@...2108...> > wrote:
>> All,
>>
>> Any Mac users out there? This script from the User
Guide does not seem to recognize entering or leaving a
figure. Any thoughts?
>
> My Mac is currently dead, but I developed these events
while I was a
> mac user and so am pretty sure they worked. It
is more likely a
> specific backend problem than a Mac vs non-Mac problem
(eg it may be
> specific to the macosx backend but not a problem for
macs in general)
>
> http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend
>
> Could you try running the script with the "use"
directive at the top
> of your script (before any other mpl code) for
different backends, eg
> tkagg vs macosx vs wxagg
>
> import matplotlib
> matplotlib.use('tkagg') # and also try
macosx and wxagg
>
> so we can see exactly where the problem is
arising. Also, run your
> script with --verbose-helpful and report the debugging
output.
> Looking at the code, it appears the enter_notify_event
and
> leave_notify_event are only defined for wx, qt and gtk
currently, and
> so need to be implemented for tk and macosx.
>
> JDH

we don't make any assumptions about what kind of object the gui event
is. We provide the GUI event because sometimes when using a specific
backend, the user wants to drill into the GUI native event (eg a
button press event) but we don't use it anywhere in the mpl frontend
because this would break the abstraction. So if you have some event
that is being fired at the UI level on figure enter, pass that in.

It looks like you may be having a problem because the
leave_notify_event is getting called more than once, or is called for
a figure that has not been entered. Check the logic in
backend_bases.FigureCanvasBase.leave_notify_event

    def leave_notify_event(self, guiEvent=None):
        """
        Backend derived classes should call this function when leaving
        canvas

        *guiEvent*
            the native UI event that generated the mpl event

        """
        self.callbacks.process('figure_leave_event', LocationEvent.lastevent)
        LocationEvent.lastevent = None

It looks like your figure_leave_event is being triggered with
LocationEvent.lastevent = None (so it is not a problem with your
guiEvent). This could happen if a leave event was processed *before*
and enter event (which sets the lastevent), or if a leave event was
processed twice.

Hopefully this will help you drill down into the source of the problem

JDH

···

On Mon, Feb 15, 2010 at 6:50 AM, Michiel de Hoon <mjldehoon@...9...> wrote:

I almost have a solution for this for the Mac OS X backend. I am stuck though at what I should pass to enter_notify_event and leave_notify_event for the guiEvent:

def leave_notify_event(self, guiEvent=None):
"""
Backend derived classes should call this function when leaving
canvas

   \*guiEvent\*
       the native UI event that generated the mpl event

   &quot;&quot;&quot;

What are the requirements for guiEvent? If I call leave_notify_event without guiEvent, so guiEvent = None, then the example gives me the following error:

Thanks! That was at least part of the problem.
I've committed a bug fix to the svn repository.

Thanks gain,

--Michiel.

···

--- On Mon, 2/15/10, John Hunter <jdh2358@...287...> wrote:

From: John Hunter <jdh2358@...287...>
Subject: Re: [Matplotlib-users] Enter Figure on Macs
To: "Michiel de Hoon" <mjldehoon@...9...>
Cc: "David Arnold" <dwarnold45@...2108...>, matplotlib-users@...1753...forge.net
Date: Monday, February 15, 2010, 8:07 AM
On Mon, Feb 15, 2010 at 6:50 AM, > Michiel de Hoon <mjldehoon@...9...> > wrote:
> I almost have a solution for this for the Mac OS X
backend. I am stuck though at what I should pass to
enter_notify_event and leave_notify_event for the guiEvent:
>
> def leave_notify_event(self, guiEvent=None):
> """
> Backend derived classes should call this
function when leaving
> canvas
>
> *guiEvent*
> the native UI event that generated
the mpl event
>
> """
>
> What are the requirements for guiEvent? If I call
leave_notify_event without guiEvent, so guiEvent = None,
then the example gives me the following error:

we don't make any assumptions about what kind of object the
gui event
is. We provide the GUI event because sometimes when
using a specific
backend, the user wants to drill into the GUI native event
(eg a
button press event) but we don't use it anywhere in the mpl
frontend
because this would break the abstraction. So if you
have some event
that is being fired at the UI level on figure enter, pass
that in.

It looks like you may be having a problem because the
leave_notify_event is getting called more than once, or is
called for
a figure that has not been entered. Check the logic
in
backend_bases.FigureCanvasBase.leave_notify_event

def leave\_notify\_event\(self, guiEvent=None\):
    &quot;&quot;&quot;
    Backend derived classes should

call this function when leaving
canvas

    \*guiEvent\*
        the native UI

event that generated the mpl event

    &quot;&quot;&quot;

self.callbacks.process('figure_leave_event',
LocationEvent.lastevent)
LocationEvent.lastevent = None

It looks like your figure_leave_event is being triggered
with
LocationEvent.lastevent = None (so it is not a problem with
your
guiEvent). This could happen if a leave event was
processed *before*
and enter event (which sets the lastevent), or if a leave
event was
processed twice.

Hopefully this will help you drill down into the source of
the problem

JDH