[Matplotlib-users] detect axes belonging to a colorbar

Benjamin Root ben.v.root at gmail.com
Fri Aug 19 13:04:00 EDT 2016


mpl.colorbar.ColorbarBase is not a subclass of Artist, so findobj() doesn't
detect that you want to match against that object type.

On Fri, Aug 19, 2016 at 12:58 PM, Joseph Fox-Rabinovitz <
jfoxrabinovitz at gmail.com> wrote:

> On a related note, I was thinking that doing something like
> `fig.findobj(mpl.colorbar.ColorbarBase)` would help you, but it raises an
> error instead:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/artist.py",
> line 975, in findobj
>     if matchfunc(c):
>   File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/colorbar.py",
> line 307, in __init__
>     self.set_label(label)
>   File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/colorbar.py",
> line 459, in set_label
>     self._set_label()
>   File "/home/jfoxrabi/miniconda3/lib/python3.5/site-packages/matplotlib/colorbar.py",
> line 448, in _set_label
>     self.ax.set_ylabel(self._label, **self._labelkw)
> AttributeError: 'Rectangle' object has no attribute 'set_ylabel'
>
> What is wrong about the way I am trying to use `findobj`?
>
> Here is the code I was testing with, for reference:
>
> import numpy as np
> import matplotlib as mpl
> from matplotlib import pyplot as plt
> x = np.arange(10)
> y = np.arange(10)
> X, Y = np.meshgrid(x, y)
> z = X * Y
> ax = plt.pcolor(X, Y, z)
> plt.colorbar()
> fig = ax.figure
> fig.findobj(mpl.colorbar.ColorbarBase)
>
>
>
>     -Joe
>
>
> On Fri, Aug 19, 2016 at 10:26 AM, Benjamin Root <ben.v.root at gmail.com>
> wrote:
>
>> No, I don't think there is a way, really. From the perspective of the
>> figure object, the colorbar looks just like another axes object. There
>> might be a trick that might work in some cases, but they would be highly
>> fragile. It would be better to just capture the colorbar object directly,
>> somehow.
>>
>> Ben Root
>>
>> On Wed, Aug 17, 2016 at 5:01 AM, <aeuii at posteo.de> wrote:
>>
>>> Hi,
>>>
>>> I would like to iterate over fig.axes and apply some changes to regular
>>> axes and skip those belonging to colorbars.
>>>
>>> What's the best way (having only fig object) to detect if an axis
>>> belongs to a colorbar or not?
>>>
>>> Thanks for an advice.
>>>
>>> Best regards,
>>> Stefan
>>>
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users at python.org
>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>
>>
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users at python.org
>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160819/2f50145a/attachment.html>


More information about the Matplotlib-users mailing list