Colorbar with diamond-shape blocks

17 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-1-24
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 2020-1-24
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

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-1-24
编辑:Adam Danz 2020-1-24
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 个评论
Anindya G
Anindya G 2020-2-6
Excellent solution! I am very grateful!
Adam Danz
Adam Danz 2020-2-6
Thanks! I like method 1 the best, although method 2 gives you more flexibility with spacing etc.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by