newcolorbar documentation

The newcolorbar allows multiple colormaps and colorbars to be used in the same subplot.

This function works by creating a new set of invisible axes matching the size and extents of the current axes. This allows a second colormap to be used in such a way that it is perceived to be two separate colormaps and two colorbars in the same axes.

Back to Climate Data Tools Contents.



[cb2,ax2,ax1] = colorbar(...)


colorbar creates a new set of axes and a new colorbar in the default (right) location.

colorbar(Location) specifies location of the new colorbar as 'North' inside plot box near top 'South' inside bottom 'East' inside right 'West' inside left 'NorthOutside' outside plot box near top 'SouthOutside' outside bottom 'EastOutside' outside right (default) 'WestOutside' outside left

colorbar(...,PropertyName,PropertyValue) specifies additional name/value pairs for colorbar.

[cb2,ax2,ax1] = colorbar(...) returns handles of the new colorbar cb2, the new axes ax2, and the previous current axes ax1.

Example 1

Let's plot some parula-colored scattered data atop a grayscale gridded dataset. Start by plotting the gridded data. We'll use the inbuilt peaks dataset for this example:

shading interp

The newcolorbar function differs from Matlab's colorbar function in that we have to create a newcolorbar before plotting the new color-scaled dataset. We can create a new colorbar quite simply:


Then plot some random color-scaled scattered data: Plot a scattered dataset with parula colormap:


Example 2

Now let's repeat Example 1, but add a little formatting. Use cmocean to create the lovely colormaps:

shading interp
cmocean algae
cb1 = colorbar('southoutside');
xlabel(cb1,'colorbar for peaks data')

Add a new colorbar toward the bottom inside of the current axes and specify blue text:

cb2 = newcolorbar('south','color','blue');

Plot scattered data and label the new colorbar:

cmocean balance
caxis([-10 10]) % sets scattered colorbar axis
xlabel(cb2,'colorbar for scattered data','color','blue')

Example 3: Three colorbars

Here's an example of three colorbars:

shading interp
cmocean algae
cb1 = colorbar;
ylabel(cb1,'colorbar for background peaks data')

cb2 = newcolorbar('southoutside') ;
cmocean amp
caxis([-10 10]) % sets scattered colorbar axis
xlabel(cb2,'colorbar for scattered data')

cb3 = newcolorbar('south');
shading interp
xlabel(cb3,'colorbar for peaks inset')

Author Info

The newcolorbar function was written by Chad A. Greene of the University of Texas at Austin's Institute for Geophysics (UTIG), August 2015. Updated for the Climate Data Toolbox in 2019.