Colorbar with diamond-shape blocks
조회 수: 10 (최근 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.
However, I want to plot the colorbar in which each block has a diamond shape as shown in the following eample:
Is it possible to customize the shape of the grid boxes like this?
Any help will be greatly appreciated.
Regards,
AG
댓글 수: 2
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
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
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);
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
Adam Danz
2020년 2월 6일
Thanks! I like method 1 the best, although method 2 gives you more flexibility with spacing etc.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!