Custom colorbar labeling centered on colors

11 visualizaciones (últimos 30 días)
John Cruce
John Cruce el 18 de Mayo de 2024
Comentada: Voss el 20 de Mayo de 2024
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?

Respuesta aceptada

Voss
Voss el 18 de Mayo de 2024
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 comentarios
John Cruce
John Cruce el 20 de Mayo de 2024
This is extremely elegant and a tremendous help. Thank you!
Voss
Voss el 20 de Mayo de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Athanasios Paraskevopoulos
Athanasios Paraskevopoulos el 18 de Mayo de 2024
Editada: Athanasios Paraskevopoulos el 18 de Mayo de 2024
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', '');

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by