Colorbar with diamond-shape blocks

10 views (last 30 days)
Anindya G
Anindya G on 24 Jan 2020
Commented: Adam Danz on 6 Feb 2020
Hello,
I want to plot a colorbar with customized shapes. When I execute the colorbar command, I get the following colorbar in which each block is rectangular-shaped.
Screen Shot 2020-01-24 at 2.34.49 PM.png
However, I want to plot the colorbar in which each block has a diamond shape as shown in the following eample:
Screen Shot 2020-01-24 at 2.34.23 PM.png
Is it possible to customize the shape of the grid boxes like this?
Any help will be greatly appreciated.
Regards,
AG
  2 Comments
Rik
Rik on 24 Jan 2020
Colorbars used to be axes objects. You can take that as an inspiration and create your own replacement for the colorbar that uses a separate axes object and a bunch of calls to patch.
As far as I'm aware (especially in the more modern releases) it is not possible to achieve what you want with a colorbar object.
Spencer Chen
Spencer Chen on 24 Jan 2020
I don't think you get that with vanilla Matlab. One way is to create your own colorbar axes and plot 'd' diamond markers.
Blessings,
Spencer

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 24 Jan 2020
Edited: Adam Danz on 24 Jan 2020
Here are two methods to create a custom legend.
Method 1: Plot dummy variables that only appear in the legend.
plot(nan,nan,'d') will produce a point in the legend but won't appear in the axes.
clf()
axes() % main axis
hold on % important!
nMarkers = 10; % number of markers/colors
labels = 5:5:50; % legend labels
colors = jet(nMarkers); % Color of each marker (by row)
sh = arrayfun(@(i)scatter(nan,nan,50,colors(i,:),... % plot invisible markers
'd','filled','DisplayName',num2str(labels(i))),...
1:nMarkers);
legend(sh) % Specify invisible marker handles
If you'd rather use plot() instead of scatter(),
sh = arrayfun(@(i)plot(nan,nan,'d','Color','none',...
'MarkerFaceColor',colors(i,:),'DisplayName',...
num2str(labels(i))),1:nMarkers);
200124 181643-MATLAB Online R2019b.png
Method 2: Create a pseudo-legend on a separate axes
Produce a 2nd axes and use text() to label points.
clf()
axes('Position',[.1 .1 .7 .7]) % main axes
cbax = axes('position',[.83 .1 .05 .7]); % color legend axes
nMarkers = 10; % number of markers/colors
y = linspace(0.4,1,nMarkers); % y value of each marker
x = zeros(size(y)); % x value of each marker
labels = strsplit(strtrim(num2str(5:5:50))); % "legend" strings
colors = jet(nMarkers); % Color of each marker (by row)
scatter(cbax,x,y,50,colors,'d','filled') % plot the colored diamonds
ylim(cbax,[0,1]) % Scale the y axis for spacing
text(cbax,x+.4,y,labels,'FontSize',8) % Add the "legend" strings
axis(cbax,'off') % Turn off the legend axis
  4 Comments
Anindya G
Anindya G on 6 Feb 2020
Excellent solution! I am very grateful!
Adam Danz
Adam Danz on 6 Feb 2020
Thanks! I like method 1 the best, although method 2 gives you more flexibility with spacing etc.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!