Custom colorbar labeling centered on colors

11 ビュー (過去 30 日間)
John Cruce
John Cruce 2024 年 5 月 18 日
コメント済み: Voss 2024 年 5 月 20 日
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?

採用された回答

Voss
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 件のコメント
John Cruce
John Cruce 2024 年 5 月 20 日
This is extremely elegant and a tremendous help. Thank you!
Voss
Voss 2024 年 5 月 20 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Athanasios Paraskevopoulos
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', '');

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by