Sitelet https://github.com/SHTOOLS/SHTOOLS/issues/272
Skip to content

Add option to change the length of colorbars #272

Description

@MarkWieczorek

Feature

The methods SHCoeffs.plot_spectrum2d() and SHGrid.plot() have the option to plot a colorbar. As part of the optional arguments, the user can change the placement, the offset from the grid, and the width. One option that is missing is to change the colorbar length (which is now equal to the length of the plot's axis). It should be possible to change the length of the colorbar (either as a percentage or physical length) by using an option cb_length.

The problem

Colorbars in pyshtools are drawn using the following procedure: First, create the image, make the axis locatable, add a new axis, and then plot the colorbar in the new axis. This ensures that the colorbar has the same length as the plot axis, and keeps this proportion when scaled.

from mpl_toolkits.axes_grid1 import make_axes_locatable 

# fake data
data = np.random.rand(10,10)
extent = (-0.5, 10.5, -0.5, 10.5)

fig = plt.figure()
axes = fig.add_subplot(111)
cim = axes.imshow(data, extent=extent)
divider = make_axes_locatable(axes)
cax = divider.append_axes('right', size='2.5%', pad=0.2)

cbar = plt.colorbar(cim, cax=cax, orientation='vertical')

test2
Unfortunately, using the optional arguement shrink with plt.colorbar does nothing here.

The closest I have come is with the following variant: After creating the new axis, add an inset to this axis with a 100% width and specified relative height.

from mpl_toolkits.axes_grid1 import make_axes_locatable 
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

# fake data
data = np.random.rand(10,10)
extent = (-0.5, 10.5, -0.5, 10.5)

fig = plt.figure()
axes = fig.add_subplot(111)
cim = axes.imshow(data, extent=extent)
divider = make_axes_locatable(axes)
cax = divider.append_axes('right', size='2.5%', pad=0.2)

cax.set_visible(False)
iax = inset_axes(cax, width='100%', height='75%', loc='center left', borderpad=0)
cbar = plt.colorbar(cim, cax=iax, orientation='vertical')

test

This kind of works, but it feels somewhat hacky (but then again, all matplotlib colorbars are hacks). However, if you then try to clean up the plot using fig.tight_layout(), you get the following warning.

fig.tight_layout()
<ipython-input-7-af72a2a08717>:1: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.

So, I could try to turn this warning off somehow, but I suspect that there is better way of doing this.

edit

This works for turning off this specific warning. Nevertheless, I still don't think that the above solution is the right one...

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=UserWarning)
    fig.tight_layout()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions