Editor's Note: This file was a File Exchange Pick of the Week
This m-file produces a colorbar with solid blocks of color between the contour levels that correspond precisely to the colors in the filled contour plot. This is in contrast to the standard MATLAB colorbar which simply plots all the colors in the colormap and, therefore, only approximates what is being plotted in the filled contour plot.
Blair Greenan (2021). colorbarf (https://www.mathworks.com/matlabcentral/fileexchange/1135-colorbarf), MATLAB Central File Exchange. Retrieved .
Inspired: cbarf
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
great code, simple and does what it claims to do. Thanks
if there is any NaN in the variable, the colorbar shows white block. I didn't find a way to remove the block except Photoshop.
does it support log scale? Like colorbar('Yscale','log')? I know I can convert the data to log10 first, but the ticks of colorbarf becomes -2 -1 0 1 2, which I want it display as 10^{-2} 10^{-1} 10^{0} 10^{1} 10^{2}, like in Latex. Thx
can colourbarf be used with pcolor instead of contourf? if so what modifications would have to be made?
A really useful function for a much better colorbar option. This is probably a simple change, but i don't know enough about handle graphics. When i change the position of the colorbar from cbarf to somewhere in the plot, cbarf is always behind the main plot. I tried changing 'Layer' to top (and the main plot to bottom) but to no avail. if anyone has what is probably a quick solution, i'd appreciate it.
Exactly what I was looking for. I don't understand why it was not built-in. Thanks.
Every time I plot a slightly different subset of my array (e.g from contourf(y(50:150),x(50:150),positron(50:150,50:150)) to contourf(y(70:130),x(70:130),positron(70:130,70:130)) ) the axis get smaller but the colorbarf remains the same size. As a consequence, if I am optimising the display of my figure, I have to keep closing the figure window.
Can this utility be modified to work with the surf function?
I need a color bar on a surface plot but the scale on the built in colorbar is inaccurate since it's divided into 64 segments instead of the 5 integer divisions that I need.
This program was not working when I downloaded it. I would get the error:
Error in ==> colorbarf at 151
colors(j) = get(H(j),'CData'); % get the color used to fill the
patches
This was with version 7 even if I used the 'v6' option.
I made these changes and it works now:
%Modded by R Pearson
for j = 1:length(H)
% colors(j)= get(H(j),'CData'); % get the color used to fill the patches
tempcolors = get(H(j),'CData'); % get the color used to fill the patches
colors(j)=tempcolors(1,1)
end
%%%%%End Pearson
I worked after this but I don't know if it is the right way to do this. I don't endorse this method at all. It simply works for me in my limited usage.
Very handy and expressive for presentations
Very good.