|
From: Nico S. <nic...@gm...> - 2010-02-15 17:04:18
|
Hi,
when plotting a color bar with a plot in matplotlib, the color bar
gets treated internally as Axes.
With two main plots, each of which comes with a color bar, one structurally gets
<class 'matplotlib.figure.Figure'>
<class 'matplotlib.axes.Axes'>
<class 'matplotlib.axes.Axes'>
<class 'matplotlib.axes.Axes'>
<class 'matplotlib.axes.Axes'>
(that is, a Figure has for childres Axes). To find out which one of
those is a color bar, I basically inspect their children an look for
Arrays with shape (256,), which is what color bars look like. That's
ugly of course, but it kind of works(tm). :)
I'm having problems, though, with associating color bars with the
specific plot. Can I rely on the rule that an Axes -- if it has a
color bar --, is immediately followed by the corresponding (color bar)
Axes environment? Are there any other properties I could check to
identify color bars? (Tried get_label to no avail.)
Cheers,
Nico
|
|
From: Jae-Joon L. <lee...@gm...> - 2010-02-15 17:17:02
|
Is there any reason that you need to find out which axes is a color bar axes from the list of axes? Can you just keep references to colorbars you create? cbar = colorbar() cax = cbar.ax cax is the axes instance of the colobar you just created. Regards, -JJ On Mon, Feb 15, 2010 at 12:04 PM, Nico Schlömer <nic...@gm...> wrote: > Hi, > > when plotting a color bar with a plot in matplotlib, the color bar > gets treated internally as Axes. > > With two main plots, each of which comes with a color bar, one structurally gets > > <class 'matplotlib.figure.Figure'> > <class 'matplotlib.axes.Axes'> > <class 'matplotlib.axes.Axes'> > <class 'matplotlib.axes.Axes'> > <class 'matplotlib.axes.Axes'> > > (that is, a Figure has for childres Axes). To find out which one of > those is a color bar, I basically inspect their children an look for > Arrays with shape (256,), which is what color bars look like. That's > ugly of course, but it kind of works(tm). :) > > I'm having problems, though, with associating color bars with the > specific plot. Can I rely on the rule that an Axes -- if it has a > color bar --, is immediately followed by the corresponding (color bar) > Axes environment? Are there any other properties I could check to > identify color bars? (Tried get_label to no avail.) > > Cheers, > Nico > > ------------------------------------------------------------------------------ > SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, > Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW > http://p.sf.net/sfu/solaris-dev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
|
From: Nico S. <nic...@gm...> - 2010-02-15 17:25:40
|
Well, it's related to the TikZ converter I'm writing. After having created the plot, the script is of course totally oblivious to what exact commands were used. I was thinking that there is still some sort of bond between the color bar and its parent plot after their creation, e.g., for when the color map of the main plot is changed. -- Is that not the case? --Nico On Mon, Feb 15, 2010 at 6:16 PM, Jae-Joon Lee <lee...@gm...> wrote: > Is there any reason that you need to find out which axes is a color > bar axes from the list of axes? Can you just keep references to > colorbars you create? > > cbar = colorbar() > cax = cbar.ax > > cax is the axes instance of the colobar you just created. > > Regards, > > -JJ > > > On Mon, Feb 15, 2010 at 12:04 PM, Nico Schlömer > <nic...@gm...> wrote: >> Hi, >> >> when plotting a color bar with a plot in matplotlib, the color bar >> gets treated internally as Axes. >> >> With two main plots, each of which comes with a color bar, one structurally gets >> >> <class 'matplotlib.figure.Figure'> >> <class 'matplotlib.axes.Axes'> >> <class 'matplotlib.axes.Axes'> >> <class 'matplotlib.axes.Axes'> >> <class 'matplotlib.axes.Axes'> >> >> (that is, a Figure has for childres Axes). To find out which one of >> those is a color bar, I basically inspect their children an look for >> Arrays with shape (256,), which is what color bars look like. That's >> ugly of course, but it kind of works(tm). :) >> >> I'm having problems, though, with associating color bars with the >> specific plot. Can I rely on the rule that an Axes -- if it has a >> color bar --, is immediately followed by the corresponding (color bar) >> Axes environment? Are there any other properties I could check to >> identify color bars? (Tried get_label to no avail.) >> >> Cheers, >> Nico >> >> ------------------------------------------------------------------------------ >> SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, >> Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW >> http://p.sf.net/sfu/solaris-dev2dev >> _______________________________________________ >> Matplotlib-users mailing list >> Mat...@li... >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> > |
|
From: Jae-Joon L. <lee...@gm...> - 2010-02-15 17:54:17
|
On Mon, Feb 15, 2010 at 12:25 PM, Nico Schlömer <nic...@gm...> wrote: > Well, it's related to the TikZ converter I'm writing. After having > created the plot, the script is of course totally oblivious to what > exact commands were used. > I was thinking that there is still some sort of bond between the color > bar and its parent plot after their creation, e.g., for when the color > map of the main plot is changed. -- Is that not the case? I doubt it. As far as I can see, it is the other way around, i.e., mappables (e.g., images) know about the colorbar they are connected. But I hope some other developers can confirm (or dispute) this. For this kind of work, you need to understand some of internals of matplotlib, and I recommend you to go through the matplotlib sources. Regards, -JJ > > --Nico > > > > On Mon, Feb 15, 2010 at 6:16 PM, Jae-Joon Lee <lee...@gm...> wrote: >> Is there any reason that you need to find out which axes is a color >> bar axes from the list of axes? Can you just keep references to >> colorbars you create? >> >> cbar = colorbar() >> cax = cbar.ax >> >> cax is the axes instance of the colobar you just created. >> >> Regards, >> >> -JJ >> >> >> On Mon, Feb 15, 2010 at 12:04 PM, Nico Schlömer >> <nic...@gm...> wrote: >>> Hi, >>> >>> when plotting a color bar with a plot in matplotlib, the color bar >>> gets treated internally as Axes. >>> >>> With two main plots, each of which comes with a color bar, one structurally gets >>> >>> <class 'matplotlib.figure.Figure'> >>> <class 'matplotlib.axes.Axes'> >>> <class 'matplotlib.axes.Axes'> >>> <class 'matplotlib.axes.Axes'> >>> <class 'matplotlib.axes.Axes'> >>> >>> (that is, a Figure has for childres Axes). To find out which one of >>> those is a color bar, I basically inspect their children an look for >>> Arrays with shape (256,), which is what color bars look like. That's >>> ugly of course, but it kind of works(tm). :) >>> >>> I'm having problems, though, with associating color bars with the >>> specific plot. Can I rely on the rule that an Axes -- if it has a >>> color bar --, is immediately followed by the corresponding (color bar) >>> Axes environment? Are there any other properties I could check to >>> identify color bars? (Tried get_label to no avail.) >>> >>> Cheers, >>> Nico >>> >>> ------------------------------------------------------------------------------ >>> SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, >>> Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW >>> http://p.sf.net/sfu/solaris-dev2dev >>> _______________________________________________ >>> Matplotlib-users mailing list >>> Mat...@li... >>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >>> >> > |
|
From: Nico S. <nic...@gm...> - 2010-02-15 18:58:49
|
> As far as I can see, it is the other way around, i.e., mappables > (e.g., images) know about the colorbar they are connected. Well yeah, that'd be even better. I'll check out the API. -- Hints would still be appreciated of course. --Nico |
|
From: Nico S. <nic...@gm...> - 2010-02-16 10:26:58
|
Alright, so I dug the sources a bit and found the snippet
========================== *snip* ==========================
cb = cbar.Colorbar(cax, mappable, **kw)
def on_changed(m):
#print 'calling on changed', m.get_cmap().name
cb.set_cmap(m.get_cmap())
cb.set_clim(m.get_clim())
cb.update_bruteforce(m)
self.cbid = mappable.callbacksSM.connect('changed', on_changed)
mappable.set_colorbar(cb, cax)
========================== *snap* ==========================
I guess what happens is that a Colorbar is created, and a callback
function registered which adapts this very color bar whenever there is
a change in color maps/limits.
Well. -- I reckon that means that at the moment there's no way to tell
if a ScalarMappable has a color bar associated or not. :/ -- At least
I don't see how it would be possible to dig up on_changed( ) from the
list of callbacks and extract cb from it.
Aaand everybody: "Fea-ture request, fea-ture request!"
get_colorbar() for ScalarMappables :)
Cheers,
Nico
On Mon, Feb 15, 2010 at 7:58 PM, Nico Schlömer <nic...@gm...> wrote:
>> As far as I can see, it is the other way around, i.e., mappables
>> (e.g., images) know about the colorbar they are connected.
>
> Well yeah, that'd be even better. I'll check out the API. -- Hints
> would still be appreciated of course.
>
> --Nico
>
|
|
From: Jae-Joon L. <lee...@gm...> - 2010-02-16 13:14:11
|
set_colorbar sets colorbar attribute. So I guess you can just check if
Mappable.colorbar is None or not.
Mappable.colorbar, when set, should be a tuple whose first item is an
image for colorbar and the second item is an colorbar axes.
Regards,
-JJ
On Tue, Feb 16, 2010 at 5:26 AM, Nico Schlömer <nic...@gm...> wrote:
> Alright, so I dug the sources a bit and found the snippet
>
> ========================== *snip* ==========================
> cb = cbar.Colorbar(cax, mappable, **kw)
>
> def on_changed(m):
> #print 'calling on changed', m.get_cmap().name
> cb.set_cmap(m.get_cmap())
> cb.set_clim(m.get_clim())
> cb.update_bruteforce(m)
>
> self.cbid = mappable.callbacksSM.connect('changed', on_changed)
> mappable.set_colorbar(cb, cax)
> ========================== *snap* ==========================
>
> I guess what happens is that a Colorbar is created, and a callback
> function registered which adapts this very color bar whenever there is
> a change in color maps/limits.
> Well. -- I reckon that means that at the moment there's no way to tell
> if a ScalarMappable has a color bar associated or not. :/ -- At least
> I don't see how it would be possible to dig up on_changed( ) from the
> list of callbacks and extract cb from it.
>
> Aaand everybody: "Fea-ture request, fea-ture request!"
> get_colorbar() for ScalarMappables :)
>
> Cheers,
> Nico
>
>
>
>
> On Mon, Feb 15, 2010 at 7:58 PM, Nico Schlömer <nic...@gm...> wrote:
>>> As far as I can see, it is the other way around, i.e., mappables
>>> (e.g., images) know about the colorbar they are connected.
>>
>> Well yeah, that'd be even better. I'll check out the API. -- Hints
>> would still be appreciated of course.
>>
>> --Nico
>>
>
|
|
From: Nico S. <nic...@gm...> - 2010-02-16 14:11:19
|
Works pretty well.
I've now implemented something like
========================== *snip* ==========================
def find_associated_colorbar( obj ):
for child in obj.get_children():
try:
cbar = child.colorbar
except AttributeError:
continue
if not cbar == None: # really necessary?
# if fetch was successful, cbar contains
# ( reference to colorbar, reference to axis
containing colorbar )
return cbar[0]
return None
========================== *snip* ==========================
How did you find out about the colormap attribute? Was that by taking
a good guess in looking at the source code, or are the public
attributes of a class documented?
Cheers,
Nico
On Tue, Feb 16, 2010 at 1:18 PM, Jae-Joon Lee <lee...@gm...> wrote:
> set_colorbar sets colorbar attribute. So I guess you can just check if
> Mappable.colorbar is None or not.
> Mappable.colorbar, when set, should be a tuple whose first item is an
> image for colorbar and the second item is an colorbar axes.
>
> Regards,
>
> -JJ
>
> On Tue, Feb 16, 2010 at 5:26 AM, Nico Schlömer <nic...@gm...> wrote:
>> Alright, so I dug the sources a bit and found the snippet
>>
>> ========================== *snip* ==========================
>> cb = cbar.Colorbar(cax, mappable, **kw)
>>
>> def on_changed(m):
>> #print 'calling on changed', m.get_cmap().name
>> cb.set_cmap(m.get_cmap())
>> cb.set_clim(m.get_clim())
>> cb.update_bruteforce(m)
>>
>> self.cbid = mappable.callbacksSM.connect('changed', on_changed)
>> mappable.set_colorbar(cb, cax)
>> ========================== *snap* ==========================
>>
>> I guess what happens is that a Colorbar is created, and a callback
>> function registered which adapts this very color bar whenever there is
>> a change in color maps/limits.
>> Well. -- I reckon that means that at the moment there's no way to tell
>> if a ScalarMappable has a color bar associated or not. :/ -- At least
>> I don't see how it would be possible to dig up on_changed( ) from the
>> list of callbacks and extract cb from it.
>>
>> Aaand everybody: "Fea-ture request, fea-ture request!"
>> get_colorbar() for ScalarMappables :)
>>
>> Cheers,
>> Nico
>>
>>
>>
>>
>> On Mon, Feb 15, 2010 at 7:58 PM, Nico Schlömer <nic...@gm...> wrote:
>>>> As far as I can see, it is the other way around, i.e., mappables
>>>> (e.g., images) know about the colorbar they are connected.
>>>
>>> Well yeah, that'd be even better. I'll check out the API. -- Hints
>>> would still be appreciated of course.
>>>
>>> --Nico
>>>
>>
>
|
|
From: Jae-Joon L. <lee...@gm...> - 2010-02-16 15:18:50
|
The last line of the "on_changed" method you posted is
mappable.set_colorbar(cb, cax)
And "set_colorbar" sets the colorbar attribute.
-JJ
On Tue, Feb 16, 2010 at 8:46 AM, Nico Schlömer <nic...@gm...> wrote:
> Works pretty well.
> I've now implemented something like
>
> ========================== *snip* ==========================
> def find_associated_colorbar( obj ):
> for child in obj.get_children():
> try:
> cbar = child.colorbar
> except AttributeError:
> continue
> if not cbar == None: # really necessary?
> # if fetch was successful, cbar contains
> # ( reference to colorbar, reference to axis
> containing colorbar )
> return cbar[0]
> return None
> ========================== *snip* ==========================
>
> How did you find out about the colormap attribute? Was that by taking
> a good guess in looking at the source code, or are the public
> attributes of a class documented?
>
> Cheers,
> Nico
>
>
>
> On Tue, Feb 16, 2010 at 1:18 PM, Jae-Joon Lee <lee...@gm...> wrote:
>> set_colorbar sets colorbar attribute. So I guess you can just check if
>> Mappable.colorbar is None or not.
>> Mappable.colorbar, when set, should be a tuple whose first item is an
>> image for colorbar and the second item is an colorbar axes.
>>
>> Regards,
>>
>> -JJ
>>
>> On Tue, Feb 16, 2010 at 5:26 AM, Nico Schlömer <nic...@gm...> wrote:
>>> Alright, so I dug the sources a bit and found the snippet
>>>
>>> ========================== *snip* ==========================
>>> cb = cbar.Colorbar(cax, mappable, **kw)
>>>
>>> def on_changed(m):
>>> #print 'calling on changed', m.get_cmap().name
>>> cb.set_cmap(m.get_cmap())
>>> cb.set_clim(m.get_clim())
>>> cb.update_bruteforce(m)
>>>
>>> self.cbid = mappable.callbacksSM.connect('changed', on_changed)
>>> mappable.set_colorbar(cb, cax)
>>> ========================== *snap* ==========================
>>>
>>> I guess what happens is that a Colorbar is created, and a callback
>>> function registered which adapts this very color bar whenever there is
>>> a change in color maps/limits.
>>> Well. -- I reckon that means that at the moment there's no way to tell
>>> if a ScalarMappable has a color bar associated or not. :/ -- At least
>>> I don't see how it would be possible to dig up on_changed( ) from the
>>> list of callbacks and extract cb from it.
>>>
>>> Aaand everybody: "Fea-ture request, fea-ture request!"
>>> get_colorbar() for ScalarMappables :)
>>>
>>> Cheers,
>>> Nico
>>>
>>>
>>>
>>>
>>> On Mon, Feb 15, 2010 at 7:58 PM, Nico Schlömer <nic...@gm...> wrote:
>>>>> As far as I can see, it is the other way around, i.e., mappables
>>>>> (e.g., images) know about the colorbar they are connected.
>>>>
>>>> Well yeah, that'd be even better. I'll check out the API. -- Hints
>>>> would still be appreciated of course.
>>>>
>>>> --Nico
>>>>
>>>
>>
>
|