GPSmaster
GPSmaster

Reputation: 914

Matlab colorbar with two axes

I'm using the mapping toolbox to create a contour plot with contourfm() contourcbar() and caxis() to label the colorbar. What is the best way to add a second axis to the colorbar so that it looks something like this?

A mock up of a two-axis colorbar

If it makes a difference, I'm running Matlab v2014b on RHEL6 with -nodesktop and -nodisplay. Thanks in advance!

Edit 1: I used Benoit_11's answer to come up with some example code to reproduce the problem. Wait for the plot to be created, then re-size the figure window. You'll see that the second colorbar axis (left side) doesn't scale properly with the rest of the plot. Any advice to fix this would greatly appreciated!

figure('Color','white')
worldmap('north america')
load topo
R = georasterref('RasterSize',[180 360], 'Latlim',[0 80],'Lonlim',[-160 -50]);

contourfm(topo, R, -7000:1000:3000)
caxis([-8000 4000])


hBar1 = contourcbar
contourcmap('jet')
caxis([1 10])
BarPos = get(hBar1,'position');
ylabel(hBar1,'Total electron content','FontSize',12);
haxes = axes('position',BarPos,'color','none','ytick',0:5:15,'ylim',[0 
15],'xtick',[]);
ylabel('Approximate position error','FontSize',12)

Upvotes: 1

Views: 3681

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

You can create a 2nd axes with the same position as the 1st colorbar and set its ylabel property to the title you want plus the ytick property to what you need.

Dummy example:

%// Create colorbar
hBar1 = colorbar;

%// Set its ylabel property
ylabel(hBar1,'Total electron content','FontSize',12);

%// Get its position
BarPos = get(hBar1,'position');

%// Create an axes at the same position
haxes = axes('position',BarPos,'color','none','ytick',0:5:15,'ylim',[0 
15],'xtick',[]);

%// Set its ylabel property
ylabel('Approximate position error','FontSize',12)

output (cropped):

enter image description here

You just need to figure out the correct placement of the colorbar so that it does not overlap with the axes containing your plots. Note that the black line at the bottom left comes from the empty axes so it won't be there in your case.

Hope that helps!

Upvotes: 3

Related Questions