Clear Filters
Clear Filters

add arrow above textbox

26 views (last 30 days)
Amal MM
Amal MM on 10 Oct 2024 at 12:39
Commented: Star Strider on 10 Oct 2024 at 14:29
Hello
I creat a colorbar depnd on my data I want to add an arrow and anothe text above a text of this figure here my code put it not work with arrow can help me to fix it
% Create a new figure
figure;
dataValues = [ 0,0.1,0.2,0.3,0.35,0.4,0.45,0.6,0.65,0.7,0.75,0.8,0.85,0.95];%15
% Number of rectangles
numRectangles = length(dataValues);
% Different strings for each rectangle
labels = {'l','Above -5','-10 - -5','-15 - -10','-25 - -20','-30 - -25', '-35 - -30',...
'-40 - -35 ' , '-45 - -40 ', '-50 - -45','-55 - -50','-60 - -55','-65 - -60',...
'-70 - -65','Below -70 ','Below -70 '};
% Define the size of the rectangles
rectWidth = 0.3;
rectHeight = 0.3;
% Define the number of rectangles
x = 14;
% Define the starting position for the first rectangle
startX = 1;
startY = 1;
% Create a colormap
cmap = parula(numRectangles); % Generate a colormap with a number of colors
% Loop through each data value to plot rectangles
hold on; % Retain current plot
for i = 1:x;
% Calculate position for each rectangle
position = [1,1 + i * (rectWidth + 0.1), rectWidth, rectHeight]; % Stacked horizontally
% Get color based on data value
colorIndex = round(dataValues(i) * (numRectangles - 1)) + 1; % Scale to colormap index
rectangle('Position', position, 'FaceColor', cmap(colorIndex+1, :)); % Plot each rectangle
% Define text label from the labels array
labelString = labels {i + 1};
% Position for the text (next to the rectangle)
textX = position(1) + rectWidth + 0.1; % Right of the rectangle
textY = position(2) + rectHeight / 2; % Centered vertically
% Place the text label next to the rectangle
text(textX, textY, labelString, 'VerticalAlignment', 'middle', 'FontSize', 12);
end
hold off; % Release the plot hold
% Set the axes limits for better visibility
xlim([0 2]);
ylim([0 8.5]);
grid on
axis on; % Turn off the axes
% Add a title (optional)
%title('Matching Colors with Data Values');
% Add a text box above the rectangles
annotation('textbox', [0.7, 0.85, 0.000002, 0.000001], 'String', 'Depth', ...
'EdgeColor', 'none', 'FontSize', 12, 'HorizontalAlignment', 'right');
% Add an arrow annotation above the text
% Calculate the arrow's position
arrowX = [textX, textX]; % X positions for the arrow
arrowY = [textY + 0.1, textY + 0.2]; % Y positions for the arrow
% Create the arrow annotation
annotation('arrow', arrowX, arrowY, 'Color', 'k', 'LineWidth', 1);

Accepted Answer

Star Strider
Star Strider on 10 Oct 2024 at 13:12
I wrote some simple functions to transform (x,y) coordinates to normalised values to use with annotation functions.
I added them as:
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
since they need that information as well.
They appear to work here, although you may want to experiment with slightly different values for ‘arrowX’ and ‘arrowY’.
Try this —
% Create a new figure
figure;
dataValues = [ 0,0.1,0.2,0.3,0.35,0.4,0.45,0.6,0.65,0.7,0.75,0.8,0.85,0.95];%15
% Number of rectangles
numRectangles = length(dataValues);
% Different strings for each rectangle
labels = {'l','Above -5','-10 - -5','-15 - -10','-25 - -20','-30 - -25', '-35 - -30',...
'-40 - -35 ' , '-45 - -40 ', '-50 - -45','-55 - -50','-60 - -55','-65 - -60',...
'-70 - -65','Below -70 ','Below -70 '};
% Define the size of the rectangles
rectWidth = 0.3;
rectHeight = 0.3;
% Define the number of rectangles
x = 14;
% Define the starting position for the first rectangle
startX = 1;
startY = 1;
% Create a colormap
cmap = parula(numRectangles); % Generate a colormap with a number of colors
% Loop through each data value to plot rectangles
hold on; % Retain current plot
for i = 1:x;
% Calculate position for each rectangle
position = [1,1 + i * (rectWidth + 0.1), rectWidth, rectHeight]; % Stacked horizontally
% Get color based on data value
colorIndex = round(dataValues(i) * (numRectangles - 1)) + 1; % Scale to colormap index
rectangle('Position', position, 'FaceColor', cmap(colorIndex+1, :)); % Plot each rectangle
% Define text label from the labels array
labelString = labels {i + 1};
% Position for the text (next to the rectangle)
textX = position(1) + rectWidth + 0.1; % Right of the rectangle
textY = position(2) + rectHeight / 2; % Centered vertically
% Place the text label next to the rectangle
text(textX, textY, labelString, 'VerticalAlignment', 'middle', 'FontSize', 12);
end
hold off; % Release the plot hold
% Set the axes limits for better visibility
xlim([0 2]);
ylim([0 8.5]);
grid on
axis on; % Turn off the axes
% Add a title (optional)
%title('Matching Colors with Data Values');
% Add a text box above the rectangles
annotation('textbox', [0.7, 0.85, 0.000002, 0.000001], 'String', 'Depth', ...
'EdgeColor', 'none', 'FontSize', 12, 'HorizontalAlignment', 'right');
% Add an arrow annotation above the text
% Calculate the arrow's position
arrowX = [textX, textX]; % X positions for the arrow
arrowY = [textY + 0.1, textY + 0.2]; % Y positions for the arrow
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
% Create the arrow annotation
annotation('arrow', xapf(arrowX,pos,xl), yapf(arrowY,pos,yl), 'Color', 'k', 'LineWidth', 1);
Use ‘xapf’ and ‘yapf’ in other instances as well, if you want to. Their use here should provide enough essential documentation for that.
.
  2 Comments
Amal MM
Amal MM on 10 Oct 2024 at 14:13
thanx,it is work .
Star Strider
Star Strider on 10 Oct 2024 at 14:29
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!