Colorbar with diamond-shape blocks

11 vues (au cours des 30 derniers jours)
Anindya G
Anindya G le 24 Jan 2020
Commenté : Adam Danz le 6 Fév 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 commentaires
Rik
Rik le 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 le 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

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 24 Jan 2020
Modifié(e) : Adam Danz le 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 commentaires
Anindya G
Anindya G le 6 Fév 2020
Excellent solution! I am very grateful!
Adam Danz
Adam Danz le 6 Fév 2020
Thanks! I like method 1 the best, although method 2 gives you more flexibility with spacing etc.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by