newbie question: type check?

Hi,

quick question from a Python noob:
Suppose I have an instance of an object of matplotlib, Is there any
way to check on its type, e.g., whether it is an instance of
matplotlib.axes.AxesSubplots?

Cheers!
Nico

On Monday 11 January 2010, Nico Schl�mer elucidated thus:

quick question from a Python noob:
Suppose I have an instance of an object of matplotlib, Is there any
way to check on its type, e.g., whether it is an instance of
matplotlib.axes.AxesSubplots?

Python's built-in 'isintance.'

isinstance(var, matplotlib.axes.AxesSubplots)

j

···

--
Joshua Kugler
Part-Time System Admin/Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ �ID 0x14EA086E

Hm.

print type( gca() )
print gca().__class__
print isinstance( gca(), matplotlib.axes.AxesSubplots)

yields

<class 'matplotlib.axes.AxesSubplot'>
<class 'matplotlib.axes.AxesSubplot'>
Traceback (most recent call last):
  File "./testfunctions.py", line 13, in <module>
    print isinstance( a, matplotlib.axes.AxesSubplots)
AttributeError: 'module' object has no attribute 'AxesSubplots

?Nico

···

On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler <joshua@...1552...> wrote:

On Monday 11 January 2010, Nico Schlömer elucidated thus:

quick question from a Python noob:
Suppose I have an instance of an object of matplotlib, Is there any
way to check on its type, e.g., whether it is an instance of
matplotlib.axes.AxesSubplots?

Python's built-in 'isintance.'

isinstance(var, matplotlib.axes.AxesSubplots)

j

--
Joshua Kugler
Part-Time System Admin/Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ ID 0x14EA086E

Hi Nico,

I'm sorry I cannot help you, but at least I'd like to share my findings with
you: I find the following statements to be true:
isinstance(gca(), matplotlib.axes.SubplotBase)
isinstance(gca(), matplotlib.axes.Subplot)
isinstance(gca(), matplotlib.axes.Axes)
but there is no class 'AxesSubplot' in matplotlib.axes. I think this class is
somehow dynamically generated from Axes and Subplot, but I have no idea how
this works.

Kind regards,
Matthias

···

On Tuesday 12 January 2010 11:40:21 Nico Schlömer wrote:

Hm.

print type( gca() )
print gca().__class__
print isinstance( gca(), matplotlib.axes.AxesSubplots)

yields

<class 'matplotlib.axes.AxesSubplot'>
<class 'matplotlib.axes.AxesSubplot'>
Traceback (most recent call last):
  File "./testfunctions.py", line 13, in <module>
    print isinstance( a, matplotlib.axes.AxesSubplots)
AttributeError: 'module' object has no attribute 'AxesSubplots

?Nico

On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler <joshua@...1552...> wrote:
> On Monday 11 January 2010, Nico Schlömer elucidated thus:
>> quick question from a Python noob:
>> Suppose I have an instance of an object of matplotlib, Is there any
>> way to check on its type, e.g., whether it is an instance of
>> matplotlib.axes.AxesSubplots?
>
> Python's built-in 'isintance.'
>
> isinstance(var, matplotlib.axes.AxesSubplots)
>
> j
>
> --
> Joshua Kugler
> Part-Time System Admin/Programmer
> http://www.eeinternet.com
> PGP Key: http://pgp.mit.edu/ ID 0x14EA086E

Hi Nico,

If you're using IPython then you can do a cool trick. Say your your instantiation is called var. You can type:

var?

and it'll spit out some info about the object, including what it's an instance of. If you type

var??

it'll try to print out more detailed information.

Hope that helps.

Regards,
-- Damon

···

--------------------------
Damon McDougall
Mathematics Institute
University of Warwick
Coventry
CV4 7AL
d.mcdougall@...831...

On 12 Jan 2010, at 10:40, Nico Schlömer wrote:

Hm.

print type( gca() )
print gca().__class__
print isinstance( gca(), matplotlib.axes.AxesSubplots)

yields

<class 'matplotlib.axes.AxesSubplot'>
<class 'matplotlib.axes.AxesSubplot'>
Traceback (most recent call last):
File "./testfunctions.py", line 13, in <module>
   print isinstance( a, matplotlib.axes.AxesSubplots)
AttributeError: 'module' object has no attribute 'AxesSubplots

?Nico

On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler <joshua@...1552...> wrote:

On Monday 11 January 2010, Nico Schlömer elucidated thus:

quick question from a Python noob:
Suppose I have an instance of an object of matplotlib, Is there any
way to check on its type, e.g., whether it is an instance of
matplotlib.axes.AxesSubplots?

Python's built-in 'isintance.'

isinstance(var, matplotlib.axes.AxesSubplots)

j

--
Joshua Kugler
Part-Time System Admin/Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ ID 0x14EA086E

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Well, I guess that's good enough for me. :slight_smile:

It's a bit unfortunate that the type() function wouldn't spit out this
information, though. When for example iterating through the output of
get_children() (iterating through a list of objects of unknown classes
that is), would there be any other way (function, method?) to get more
info on the matplotlib object?

Cheers,
Nico

···

On Tue, Jan 12, 2010 at 12:01 PM, Matthias Michler <MatthiasMichler@...361...> wrote:

Hi Nico,

I'm sorry I cannot help you, but at least I'd like to share my findings with
you: I find the following statements to be true:
isinstance(gca(), matplotlib.axes.SubplotBase)
isinstance(gca(), matplotlib.axes.Subplot)
isinstance(gca(), matplotlib.axes.Axes)
but there is no class 'AxesSubplot' in matplotlib.axes. I think this class is
somehow dynamically generated from Axes and Subplot, but I have no idea how
this works.

Kind regards,
Matthias

On Tuesday 12 January 2010 11:40:21 Nico Schlömer wrote:

Hm.

print type( gca() )
print gca().__class__
print isinstance( gca(), matplotlib.axes.AxesSubplots)

yields

<class 'matplotlib.axes.AxesSubplot'>
<class 'matplotlib.axes.AxesSubplot'>
Traceback (most recent call last):
File "./testfunctions.py", line 13, in <module>
print isinstance( a, matplotlib.axes.AxesSubplots)
AttributeError: 'module' object has no attribute 'AxesSubplots

?Nico

On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler <joshua@...1552...> > wrote:
> On Monday 11 January 2010, Nico Schlömer elucidated thus:
>> quick question from a Python noob:
>> Suppose I have an instance of an object of matplotlib, Is there any
>> way to check on its type, e.g., whether it is an instance of
>> matplotlib.axes.AxesSubplots?
>
> Python's built-in 'isintance.'
>
> isinstance(var, matplotlib.axes.AxesSubplots)
>
> j
>
> --
> Joshua Kugler
> Part-Time System Admin/Programmer
> http://www.eeinternet.com
> PGP Key: http://pgp.mit.edu/ ID 0x14EA086E

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi Nico,

Well, I guess that's good enough for me. :slight_smile:

Just to share my knowlegde with you: I found
In [18]: matplotlib.axes.Subplot.__bases__
Out[18]:
(<class matplotlib.axes.SubplotBase at 0x8a1f6bc>,
<class 'matplotlib.axes.Axes'>)

That is the class 'matplotlib.axes.Subplot' was inherited from SubplotBase and
Axes. I think the name 'AxesSubplot' belongs to the class object 'Subplot',
which is dynamically generated in the axes.py and includes the name of
the 'mother': namely 'Axes' (see also function 'subplot_class_factory' at the
end of matplotlib/axes.py).

It's a bit unfortunate that the type() function wouldn't spit out this
information, though. When for example iterating through the output of
get_children() (iterating through a list of objects of unknown classes
that is), would there be any other way (function, method?) to get more
info on the matplotlib object?

I think type just uses the '__name__' of the class, which is in most cases
really the name of the class, but I think for Subplot-instances this is a bit
more involved because of the internal structure of matplotlib.
In[11]: matplotlib.axes.Subplot.__name__
Out[11]: 'AxesSubplot'

I would expect that the case of the Subplot-objects is somehow singular and
all other mpl-objects can be classified using type and isinstance, but I'm
not an mpl-expert and maybe there are more special cases.

Kind regards,
Matthias

···

On Tuesday 12 January 2010 12:26:01 Nico Schlömer wrote:

On Tue, Jan 12, 2010 at 12:01 PM, Matthias Michler > > <MatthiasMichler@...361...> wrote:
> Hi Nico,
>
> I'm sorry I cannot help you, but at least I'd like to share my findings
> with you: I find the following statements to be true:
> isinstance(gca(), matplotlib.axes.SubplotBase)
> isinstance(gca(), matplotlib.axes.Subplot)
> isinstance(gca(), matplotlib.axes.Axes)
> but there is no class 'AxesSubplot' in matplotlib.axes. I think this
> class is somehow dynamically generated from Axes and Subplot, but I have
> no idea how this works.
>
> Kind regards,
> Matthias
>
> On Tuesday 12 January 2010 11:40:21 Nico Schlömer wrote:
>> Hm.
>>
>> print type( gca() )
>> print gca().__class__
>> print isinstance( gca(), matplotlib.axes.AxesSubplots)
>>
>> yields
>>
>> <class 'matplotlib.axes.AxesSubplot'>
>> <class 'matplotlib.axes.AxesSubplot'>
>> Traceback (most recent call last):
>> File "./testfunctions.py", line 13, in <module>
>> print isinstance( a, matplotlib.axes.AxesSubplots)
>> AttributeError: 'module' object has no attribute 'AxesSubplots
>>
>> ?Nico
>>
>> On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler > >> <joshua@...1552...> > > > > wrote:
>> > On Monday 11 January 2010, Nico Schlömer elucidated thus:
>> >> quick question from a Python noob:
>> >> Suppose I have an instance of an object of matplotlib, Is there any
>> >> way to check on its type, e.g., whether it is an instance of
>> >> matplotlib.axes.AxesSubplots?
>> >
>> > Python's built-in 'isintance.'
>> >
>> > isinstance(var, matplotlib.axes.AxesSubplots)
>> >
>> > j
>> >
>> > --
>> > Joshua Kugler
>> > Part-Time System Admin/Programmer
>> > http://www.eeinternet.com
>> > PGP Key: http://pgp.mit.edu/ ID 0x14EA086E
>
> -------------------------------------------------------------------------
>----- This SF.Net email is sponsored by the Verizon Developer Community
> Take advantage of Verizon's best-in-class app development support A
> streamlined, 14 day to market process makes app distribution fast and
> easy Join now and get one step closer to millions of Verizon customers
> http://p.sf.net/sfu/verizon-dev2dev
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> matplotlib-users List Signup and Options

Matthias Michler wrote:

I would expect that the case of the Subplot-objects is somehow singular and all other mpl-objects can be classified using type and isinstance, but I'm not an mpl-expert and maybe there are more special cases.
  

Yes. This weirdness came about because Subplot used to contain functionality for both the projections (how data is projected, ticked etc.) and positioning within the figure. Around 0.91, these two things were separated out so that writing a new projection (eg. polar axes) didn't require reimplementing the subplot infrastructure. The Subplot class was kept around as an alias for AxesSubplot (a dynamically generated class) backward compatibility.

As for not being able to do "isinstance(gca(), matplotlib.axes.SubplotAxes)" -- I'm not sure that's a problem. It would help to understand the use case, but I suspect you either want "isinstance(gca(), matplotlib.axes.Axes)" or "isinstance(gca(), matplotlib.axes.SubplotBase)".

Mike

···

Kind regards,
Matthias

On Tue, Jan 12, 2010 at 12:01 PM, Matthias Michler >> >> <MatthiasMichler@...361...> wrote:
    

Hi Nico,

I'm sorry I cannot help you, but at least I'd like to share my findings
with you: I find the following statements to be true:
isinstance(gca(), matplotlib.axes.SubplotBase)
isinstance(gca(), matplotlib.axes.Subplot)
isinstance(gca(), matplotlib.axes.Axes)
but there is no class 'AxesSubplot' in matplotlib.axes. I think this
class is somehow dynamically generated from Axes and Subplot, but I have
no idea how this works.

Kind regards,
Matthias

On Tuesday 12 January 2010 11:40:21 Nico Schl�mer wrote:
      

Hm.

print type( gca() )
print gca().__class__
print isinstance( gca(), matplotlib.axes.AxesSubplots)

yields

<class 'matplotlib.axes.AxesSubplot'>
Traceback (most recent call last):
  File "./testfunctions.py", line 13, in <module>
    print isinstance( a, matplotlib.axes.AxesSubplots)
AttributeError: 'module' object has no attribute 'AxesSubplots

?Nico

On Tue, Jan 12, 2010 at 6:45 AM, Joshua J. Kugler >>>> <joshua@...1552...> >>>> >>> wrote:
      

On Monday 11 January 2010, Nico Schl�mer elucidated thus:
          

quick question from a Python noob:
Suppose I have an instance of an object of matplotlib, Is there any
way to check on its type, e.g., whether it is an instance of
matplotlib.axes.AxesSubplots?
            

Python's built-in 'isintance.'

isinstance(var, matplotlib.axes.AxesSubplots)

j

--
Joshua Kugler
Part-Time System Admin/Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ ID 0x14EA086E
          

-------------------------------------------------------------------------
----- This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support A
streamlined, 14 day to market process makes app distribution fast and
easy Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
      
------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev _______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

As for not being able to do "isinstance(gca(), matplotlib.axes.SubplotAxes)" -- I'm not sure that's a problem. It would help to understand the use case, but I suspect you either want "isinstance(gca(), matplotlib.axes.Axes)" or "isinstance(gca(), matplotlib.axes.SubplotBase)".

You may also want to use:

issubclass(an_appropriate_superclass)

instead.

However, given the python's duck-typing semantics, you usually don't need to know exactly what class something is -- that's kind of the point of dynamic typing.

So what is it you're trying to accomplish? Maybe there is a better way.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

So what is it you're trying to accomplish? Maybe there is a better way.

Well, I'm recursively iterating through the children of all objects,
starting at gcf() (and then picking up gca(), lines, axes, everything
that belongs to the plot), which is then parsed and a TikZ file is
spit out. I need the type-checking exactly when I loop through the
children of an object to be able to say "Now we have an axes object,
plot axes.".

I'm a total Python noob, so my first attempts at an implementation is
likely quite shabby:

http://win.ua.ac.be/~nschloe/other/websvn/filedetails.php?repname=matplotlib2tikz&path=%2Ftrunk%2Fmatplotlib2tikz.py

You'll find handle_children() towards the bottom of the file.

Would the type-checking as applied in there be something sensitive to
do? Also, of course, hints of *all kinds very much appreciated!

Cheers,
Nico