Changing fontsize of colorbar labels in healpy.mollview()

3.3k Views Asked by At

I'm using healpy's mollview() function (http://healpy.github.com/healpy/generated/healpy.visufunc.mollview.html) to plot a map. I can specify a title and label for the colorbar, but I don't see how I can change the font size. Sorry if this isn't the right place to post this question... I couldn't find anywhere to ask it on healpy's project page. I also can't tag the question as "healpy" because I don't have enough reputation and no one has ever asked a question about healpy before.

4

There are 4 best solutions below

0
On

sorry, late answer, but useful if somebody finds this from google:

You can change the font size for all the text in the plot updating rcParams:

import matplotlib
matplotlib.rcParams.update({'font.size': 22})
3
On

Another late response:

Unfortunately, the rcParams doesn't work for the units problem, since that's a text object in the hp.visufunc.mollview function.

import healpy as hp
import numpy as np
import matplotlib

fontsize = 20

d = np.arange(12*16**2)
hp.mollview(d, title='Hello', unit=r'T', notext=False, coord=['G','C'])

matplotlib.rcParams.update({'font.size':fontsize})
matplotlib.pyplot.show()

incomplete

As you can see, the text objects corresponding to the unit and the coordinate system aren't affected because they just have a separate text handling system. It's possible to change the objects by using the gcf() function, i.e.

import healpy as hp
import numpy as np
import matplotlib

fontsize = 20

d = np.arange(12*16**2)
hp.mollview(d, title='Hello', unit=r'T', notext=False, coord=['G','C'])

matplotlib.rcParams.update({'font.size':fontsize})
matplotlib.pyplot.show()

f = matplotlib.pyplot.gcf().get_children()
HpxAx = f[1]
CbAx = f[2]

coord_text_obj = HpxAx.get_children()[0]
coord_text_obj.set_fontsize(fontsize)

unit_text_obj = CbAx.get_children()[1]
unit_text_obj.set_fontsize(fontsize)

matplotlib.pyplot.show()

complete

0
On

(I cannot comment on Warpig's comment due to my low reputation)

As of July 2021, I am also getting IndexError: list index out of range when calling HpxAx = f[1], with healphy=1.11.0 and matplotlib==3.0.0. My workaround was to create the figure first, and then update it:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import healpy as hp

matplotlib.rcParams.update({'font.size': 18}) # fontsize for colorbar's values
fontsize = 22
cm.magma.set_under("w") # set background to white

# create figure
d = np.arange(12*16**2)
hp.mollview(d, title='Hello', unit=r'T', notext=False, coord=['G','C'], cmap=cm.magma)

f = plt.gcf() # accessing the current figure...
CbAx = f.get_children()[2] # ... then the colorbar's elements
coord_text_obj = CbAx.get_children()[1] # [1] corresponds to the particular label of the
                                        # colorbar, i.e. "Field value" in this case
coord_text_obj.set_fontsize(fontsize)
plt.show()

Note that in this case I'm only interested in increasing the colorbar's label fontsize to 22, and the extremes of the colorbar to 18; the "Equatorial" label is not affected. If you want to save the figure, remember to do it before the plt.show().

Image link due to low reputation

0
On

This has gotten easier (yet, not optimal) by using healpy.projview.

I also make use of rcParams['font.size'] to globally set the font size; however, for me this does not work out of the box for healpy plots. Changing the font size of colorbar label and tick labels only works when also passing the keyword fontsize=dict(cbar_label='medium', cbar_tick_label='medium'). Note also the keyword override_plot_properties for changing figure width as well as colorbar size and padding.

import numpy as np
import matplotlib.pyplot as plt
import healpy as hp

plt.rcParams['font.size'] = 8
plt.rcParams['figure.figsize'] = 3.2, 2.0
fw, fh = plt.rcParams['figure.figsize']

d = np.arange(12*16**2) - 1535.5

hp.projview(
    d, coord=['G', 'C'], cmap='planck', min=-1500, max=+1500,
    override_plot_properties=dict(figure_width=fw, cbar_shrink=0.8, cbar_label_pad=7),
    cbar_ticks=np.linspace(-1500, 1500, 7), unit='cbar label',
    fontsize=dict(title='large', 
                  xlabel='medium', ylabel='medium', 
                  xtick_label='medium', ytick_label='medium', 
                  cbar_label='medium', cbar_tick_label='medium'),
)

This produces the following plot:

setting healpy mollview colorbar font size

Note, I was using matplotlib version 3.8.2 and healpy version 1.16.6 for this.