Centering Colorbar and Reducing Label Padding

I have a colorbar that I've adjusted to be roughly 80% of its original size. I'd like to center the colorbar at the bottom inside of the figure. Below is a snippet of my code. How would I go about centering the reduced size figure on the figure axes?
hcb=colorbar('south');
hcb.Label.String='Colorbar Title';
colorbarpos=hcb.Position;
colorbarpos(2)=-0.015+colorbarpos(2);
colorbarpos(4)=0.4*colorbarpos(4);
colorbarpos(3)=0.8*colorbarpos(3);
hcb.Position = colorbarpos;
Additionally, I'd like to reduce the padding (line space) between the colorbar ticklabels and colorbar title (both above the colorbar). In other words, I'd like both to be closer to the colorbar as to not take up as much of the plot space. Any tips on how to best do this?

 Accepted Answer

Changing the position can be a bit more efficient —
hcb=colorbar('south');
hcb.Label.String='Colorbar Title';
colorbarpos=hcb.Position;
hcb.Position = colorbarpos+[0 0.015 0.8 0.4].*colorbarpos;
I am not certain what you are referring to with respect to the colorbar tick labels and the colorbar title. It would help to have at least a relevant example of some working code, or perhaps an image.
.

8 Comments

Here's a sample working code:
set(gcf,'units','pixel','position',[30,30,415,415],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','k');
ax=gca;
colormap(ax,jet(70))
caxis([1, 70]);
hcb=colorbar('south');
hcb.Label.String='Colorbar Title';
hcb.Label.FontSize=6;
colorbarpos=hcb.Position;
colorbarpos(2)=-0.015+colorbarpos(2);
colorbarpos(4)=0.4*colorbarpos(4);
colorbarpos(3)=0.8*colorbarpos(3);
hcb.Position = colorbarpos;
set(hcb,'YTick',[1:1:70],'TickLength',colorbarpos(4)/colorbarpos(3));
set(hcb,'YTickLabel',{'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';},'FontSize', 6,'Color',[190/255 190/255 190/255]);
box on;
I want to center the colorbar on the bottom of the figure. I'd like the YTickLabels below the colorbar and the colorbar title above but without much as much gap between the colorbar and title/labels (getting the labels to be fairly flush on the colorbar). Hopefully that makes sense.
The colorbar title is now a separate text object, so you can adjust its (x,y) position and other properties independently (because I could not adjust its position as a colorbar property). Getting the colorbar in the correct position (or at least close) required adjusting the first element of the colorbar position property as well.
Try this —
set(gcf,'units','pixel','position',[30,30,415,415],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','k');
ax=gca;
colormap(ax,jet(70))
caxis([1, 70]);
hcb=colorbar('south');
hcb.Label.String='Colorbar Title';
text(0.5, 0.1, hcb.Label.String, 'Color','w', 'Horiz','center', 'FontSize',7) % Separate Colorbar Title (ADDED)
hcb.Label.String = ''; % <— ADDED
hcb.Label.FontSize=6;
colorbarpos=hcb.Position;
colorbarpos(1) = colorbarpos(1)+0.07; % <— ADDED
colorbarpos(2) = 0.01+colorbarpos(2); % <— CHANGED
colorbarpos(3)=0.8*colorbarpos(3);
colorbarpos(4)=0.4*colorbarpos(4);
hcb.Position = colorbarpos;
hcb.AxisLocation = 'out'; % <— ADDED
set(hcb,'YTick',[1:1:70],'TickLength',colorbarpos(4)/colorbarpos(3));
set(hcb,'YTickLabel',{'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';},'FontSize', 6,'Color',[190/255 190/255 190/255]);
box on;
This should work (with minor modification) in other axes as well.
.
This is great. Thank you.
I'm wanting to get the title and labels closer to the colorbar. My modified code looks like this:
set(gcf,'units','pixel','position',[30,30,415,415],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','k');
ax=gca;
colormap(ax,jet(70))
caxis([0, 70]);
hcb=colorbar('south');
hcb.Label.FontSize=6;
colorbarpos=hcb.Position;
text(0.5, 0.09, 'Colorbar Title', 'Color','w', 'Horiz','center', 'FontSize',7)
colorbarpos(1) = colorbarpos(1)+0.07;
colorbarpos(2) = 0.01+colorbarpos(2);
colorbarpos(4)=0.4*colorbarpos(4);
colorbarpos(3)=0.8*colorbarpos(3);
hcb.Position = colorbarpos;
hcb.AxisLocation = 'out';
set(hcb,'YTick',[0:1:70],'TickLength',colorbarpos(4)/colorbarpos(3),'LineWidth',0.4);
set(hcb,'YTickLabel',{'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';},'FontSize',6,'Color',[190/255 190/255 190/255]);
box on;
I see how to simply change the position of the title text (though would love a dynamic solution that changes with figure size). Do you have any ideas on getting the labels closer to the colorbar to look more like the second image below?
It is possible to manipulate the colorbar 'Label.Position' property. (I just couldn’t get it to work last night, for some reason, however it works now, and should be dynamic.)
set(gcf,'units','pixel','position',[30,30,415,415],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','k');
ax=gca;
colormap(ax,jet(70))
caxis([0, 70]);
hcb=colorbar('south');
hcb.Label.FontSize=6;
colorbarpos=hcb.Position;
hcb.Label.String = 'Colorbar Title';
hcb.Label.Position = hcb.Label.Position+[0 -0.025 0];
colorbarpos(1) = colorbarpos(1)+0.07;
colorbarpos(2) = 0.01+colorbarpos(2);
colorbarpos(4)=0.4*colorbarpos(4);
colorbarpos(3)=0.8*colorbarpos(3);
hcb.Position = colorbarpos;
hcb.AxisLocation = 'out';
set(hcb,'YTick',[0:1:70],'TickLength',colorbarpos(4)/colorbarpos(3),'LineWidth',0.4);
set(hcb,'YTickLabel',{'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';},'FontSize',6,'Color',[190/255 190/255 190/255]);
box on;
When I run your code, the colorbar tick labels do not rotate, and rotating them does not appear to be possible since there is no property (that I can find) that allows that. Setting or querying 'TickLabelRotation' or 'YTickLabelRotation' just throws an error. It also does not appear to be possible to change their positions.
EDIT — (1 Aug 2023 at 16:20)
This essentially does what you want, however it is extremely fragile code —
figure
set(gcf,'units','pixel','position',[30,30,415,415],'color','w');
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','k');
ax=gca;
colormap(ax,jet(70))
caxis([0, 70]);
hcb=colorbar('south');
hcb.Label.FontSize=6;
colorbarpos=hcb.Position;
hcb.Label.String = 'Colorbar Title';
hcb.Label.Position = hcb.Label.Position+[0 -0.025 0];
colorbarpos(1) = colorbarpos(1)+0.07;
colorbarpos(2) = 0.01+colorbarpos(2);
colorbarpos(4)=0.4*colorbarpos(4);
colorbarpos(3)=0.8*colorbarpos(3);
hcb.Position = colorbarpos;
hcb.AxisLocation = 'out';
set(hcb,'YTick',[0:1:70],'TickLength',colorbarpos(4)/colorbarpos(3),'LineWidth',0.4);
% set(hcb,'YTickLabel',{'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';},'FontSize',6,'Color',[190/255 190/255 190/255]);
text(hcb.YTick*0.01065+0.106, ones(size(hcb.YTick))*0.03, {'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';'';'';'';'';'55';'';'';'';'';'60';'';'';'';'';'65';'';'';'';'';'70';}, 'FontSize',6,'Color',[190/255 190/255 190/255])
hcb.YTickLabel = [];
box on;
The colorbar title is still there, it’s just difficult to see here.
.
I recently moved this colorbar over to a lat/long based map grid. I'm now unable to see the added text outside the colorbar labels and title, which show fine when not hidden.
I've tried a number of fixes but no luck yet. Any ideas what might have changed to render the text function inoperable on the new grid?
I’m not certain what the problem is, however the text call (x,y) coordinates need to reflect the change in the colorbar position (and orientation, if that also changed).
The other problem might be the lat/lon coordinate system, since it might not be compatible with the Cartesian coordinate system that originally defined the colorbar. Unfortunately, I don’t have the Mapping Toolbox (that I assume you’re using), so I’m not familiar with it or any of its functions or inherent properties. Looking through the online documentation, the Mapping Toolbox has only one such function, that being the lcolorbar function. It seems to have most of the same characteristics and properties, however is an axes object (rather than a colorbar object), so it may be significantly different.
.
Yes, the change to lat/lon coordinates is the issue. I'm still calling the colorbar and the positioning of the colorbar shows the cartesian coordinate system. I'm trying to use the position of the colorbar to position the text, but running into an impasse given the different coordinate systems.
I don't have the mapping toolbox either but use the m_map package (https://www.eoas.ubc.ca/~rich/mapug.html). The functionality is basically the same with an m_text call for text (https://github.com/g2e/m_map/blob/master/m_text.m).
My only suggestion at this point is to contact those who support the ‘m_map’ package to see what insights they may be able to offer with respect to the colorbar.
There is a section on colorbars and contour maps, and apparently a function called ‘m_contfbar’, although I have no experience with it and so cannot determine how to modify it, if any modifications are even possible. While I generally don’t suggest tweaking function code in situations such as this, creating your own version of ‘m_contfbar’ (giving it a slightly different name) that does what you want could work. I’ve not looked at any of this package, so I don’t know what language it’s written in, and if the code is compiled (that would be next to impossible to change) or exists as source code (that might be possible to change, given reasonable fluency in that specific language).
Anyway, taking a look at the ‘m_contfbar’ code would likely be the best place to begin. (I have no pressing need to download this package, however I’ll certainly keep it in mind in case I need to do any mapping projects.)
.

Sign in to comment.

More Answers (0)

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Products

Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!