Custom colorbar labeling centered on colors
조회 수: 11 (최근 30일)
이전 댓글 표시
I have a figure for which I want to create custom colorbar labeling. Below is the sample code:
figure;
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','w');
set(gcf,'units','pixel','position',[70,70,600,600],'papersize',[600,600],'color','w');
ax=gca;
colormap(flipud(jet(11)));
caxis([0 11]);
hcb=colorbar('SouthOutside');
axPos = ax.Position;
colorbarpos=hcb.Position;
hcb.Ruler.TickLabelRotation=0;
set(hcb,'YTick',[0:1:11],'TickLength',colorbarpos(4)/colorbarpos(3));
This gives a standard southoutside, reverse jet colorbar like below:
Instead, I'd like to create custom labels centered on each color...something like below:
Any ideas on how or if this can be done?
댓글 수: 0
채택된 답변
Voss
2024년 5월 18일
f = figure;
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','w');
set(f,'units','pixel','position',[70,70,600,600],'papersize',[600,600],'color','w');
colormap(flipud(jet(11)));
caxis([0 11]);
hcb=colorbar('SouthOutside','YTick',[]);
colorbarpos=hcb.Position;
ax = axes( ...
'Parent',f, ...
'Units','normalized', ...
'Position',[colorbarpos(1) 0 colorbarpos([3 2])], ...
'Visible','off', ...
'XLim',[0 1], ...
'YLim',[0 1.1]);
text( ...
'Parent',ax, ...
'Position',[0 1], ...
'String',{'Record','Warmest'}, ...
'HorizontalAlignment','left', ...
'VerticalAlignment','top')
text( ...
'Parent',ax, ...
'Position',[1 1], ...
'String',{'Record','Coldest'}, ...
'HorizontalAlignment','right', ...
'VerticalAlignment','top')
patch( ...
'Parent',ax, ...
'XData',[0.15 0.17 0.165 0.17 0.15 0.85 0.83 0.835 0.83 0.85], ...
'YData',[0.8 0.85 0.8 0.75 0.8 0.8 0.85 0.8 0.75 0.8], ...
'EdgeColor','k', ...
'FaceColor','k')
text( ...
'Parent',ax, ...
'Position',[0.2 0.8], ...
'String','Top 5 Warmest', ...
'HorizontalAlignment','left', ...
'VerticalAlignment','middle', ...
'BackgroundColor','w')
text( ...
'Parent',ax, ...
'Position',[0.8 0.8], ...
'String','Top 5 Coldest', ...
'HorizontalAlignment','right', ...
'VerticalAlignment','middle', ...
'BackgroundColor','w')
댓글 수: 8
추가 답변 (1개)
Athanasios Paraskevopoulos
2024년 5월 18일
편집: Athanasios Paraskevopoulos
2024년 5월 18일
I tried the folowing code! It is similar with your second graph, but I could replicate the arrows correct. You can edit the code and create the result yoy want
% Create a figure
figure;
% Set the axis properties
set(gca, 'xtick', [], 'xticklabel', [], 'ytick', [], 'yticklabel', [], 'color', 'w');
% Set the figure properties
set(gcf, 'units', 'pixel', 'position', [70, 70, 600, 600], 'papersize', [600, 600], 'color', 'w');
% Get the current axis handle
ax = gca;
% Set the colormap and color limits
colormap(flipud(jet(11)));
caxis([0 11]);
% Create a colorbar
hcb = colorbar('SouthOutside');
% Get axis and colorbar positions
axPos = ax.Position;
colorbarpos = hcb.Position;
% Set tick label rotation
hcb.Ruler.TickLabelRotation = 0;
% Define custom ticks and labels
customTicks = 0:1:11;
customLabels = {'', '', '', '', '', '', '', '', '', '', '', ''};
% Apply custom ticks and labels to the colorbar
set(hcb, 'Ticks', customTicks, 'TickLabels', customLabels, 'TickLength', colorbarpos(4) / colorbarpos(3));
% Add custom annotations
text('Units', 'normalized', 'Position', [0.05, -0.2], 'String', 'Record Warmest', 'FontSize', 10, 'HorizontalAlignment', 'center');
text('Units', 'normalized', 'Position', [0.30, -0.2], 'String', 'Top 5 Warmest', 'FontSize', 10, 'HorizontalAlignment', 'center');
text('Units', 'normalized', 'Position', [0.70, -0.2], 'String', 'Top 5 Coldest', 'FontSize', 10, 'HorizontalAlignment', 'center');
text('Units', 'normalized', 'Position', [0.95, -0.2], 'String', 'Record Coldest', 'FontSize', 10, 'HorizontalAlignment', 'center');
% Add arrows
annotation('textarrow', [0.05 0.00], [0.02 0.02], 'String', '');
annotation('textarrow', [0.35 0.30], [0.02 0.02], 'String', '');
annotation('textarrow', [0.65 0.70], [0.02 0.02], 'String', '');
annotation('textarrow', [0.95 1.00], [0.02 0.02], 'String', '');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!