How to set the colorbar ticks fontsize.

Hi all,

I looking for a way to modify the colorbar ticks font size.
a=rand(100,100)
imshow(a)
colorbar()
and then??

For instance, xticks(fontsize=20) works well to modify the ticks fontsize along the X-axis but colorbar(fontsize=20) does not exists.
I must be missing something.

Xavier

···

--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr�
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata@...419...
############################################

Xavier Gnata wrote:

Hi all,

I looking for a way to modify the colorbar ticks font size.
a=rand(100,100)
imshow(a)
colorbar()
and then??

For instance, xticks(fontsize=20) works well to modify the ticks fontsize along the X-axis but colorbar(fontsize=20) does not exists.
I must be missing something.

cb = colorbar() # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
     t.set_fontsize(20)

The colorbar function makes a new axes object, the "ax" attribute of the Colorbar instance returned by the colorbar function. From that you get the list of text objects, which you then modify.

The pylab xticks and yticks functions make the retrieval and modification of the text objects easier, but they operate only on the "current axes", and the colorbar leaves the image axes as current.

An alternative method is to change the current axes:

imaxes = gca()
axes(cb.ax)
yticks(fontsize=20)
axes(imaxes)

Here I saved and restored the original axes in case you want to do something with the image axes after modifying the colorbar.

Eric

Eric Firing wrote:

Xavier Gnata wrote:

Hi all,

I looking for a way to modify the colorbar ticks font size.
a=rand(100,100)
imshow(a)
colorbar()
and then??

For instance, xticks(fontsize=20) works well to modify the ticks fontsize along the X-axis but colorbar(fontsize=20) does not exists.
I must be missing something.

cb = colorbar() # grab the Colorbar instance
for t in cb.ax.get_yticklabels():
    t.set_fontsize(20)

The colorbar function makes a new axes object, the "ax" attribute of the Colorbar instance returned by the colorbar function. From that you get the list of text objects, which you then modify.

The pylab xticks and yticks functions make the retrieval and modification of the text objects easier, but they operate only on the "current axes", and the colorbar leaves the image axes as current.

An alternative method is to change the current axes:

imaxes = gca()
axes(cb.ax)
yticks(fontsize=20)
axes(imaxes)

Here I saved and restored the original axes in case you want to do something with the image axes after modifying the colorbar.

Eric

Hi,

Your first method is not working on my box (no change) but the second one is just fine :slight_smile:
However, it is not as intuitive as other matplotlib ways of doing things.
Is there a simple way to modify the colorbar code to get something like colorbar(fontsize=20) work ?
Or maybe as a method like that : cb=colorbar() cb.set_fontsize(20)
Any comments ?

Xavier

···

--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr�
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata@...419...
############################################