I am trying to color a box on a plot.

9 次查看(过去 30 天)
i have a box with text in it that moves along a line on a plot. i need there to be background color in the box under the text. Right now I can only get it to show on top of the text.

采纳的回答

Chunru
Chunru 2024-3-6
You can change the face color of the box:
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2,"FaceColor", "g");
You also need to change the order of box and text. MATLAB draw the object layer by layer. If you want to show text on top of box, you have to plot box first.
%% Header
% Written by C. Vogen
% Date Written:3/3/2024
% Modified: None
%% Variable Glossery
% t_start = 0; % seconds
% t_end = 10; % seconds
% x_final = 10; % meters
% y_final = 0; % meters
% g = 9.81 % m/s^2
% m = 150;
%
%% Code Statements
clear; clc; close all;
t_start = 0; % seconds
t_end = 10; % seconds
x_final = 10; % meters
y_final = 0; % meters
g = 9.81; % m/s^2
v_x0 = x_final/t_end; % finding initial x velocity
v_y0 = (y_final + (g*0.5*(t_end^2)))/t_end; % finding initial y velocity
m = 11;
T = zeros(m,2);
incrv = (t_end - t_start)/(m-1);
startCannon =[0,0]; endCannon = [.175,10]; % starts the cannon
Cannon = [startCannon;endCannon];
figure
plot(Cannon(:,1),Cannon(:,2),'LineWidth',5)
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
hold on
pause(1)
Color_start = [.25 .25 .25];
Color_end = [1 1 0];
Color_incv = (Color_end - Color_start)/(m-1);
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2,"FaceColor", "g");
flag_text = text(0, 0, '', 'HorizontalAlignment', 'left','VerticalAlignment','bottom', 'FontWeight', 'bold');
for i = 1:m
t = t_start +(i-1)*incrv;
x = v_x0 * t;
y = (v_y0 * t) - ((g * t^2)/2);
Color = Color_start +(i-1)*Color_incv;
pause(0.4)
if i>1
delete(ball) % removing the previous marker
delete(line) % removes the line connecting the box to the point
end
ball = plot(x,y,'o','MarkerFaceColor','r'); % plotting inside the loop
set(ball, 'XData',x, 'YData',y)
set(flag_text, 'Position', [x+0.2, 30], 'String', {['X: ', num2str(x)], ['Y: ', num2str(y)],['I: ', num2str(i)],['T: ',num2str(t)]})
set(square, 'Position', [x, 25, 2.5, 38]);
line = plot([x,x],[0,30],'b','LineWidth',2);
axis normal
title('Visual Loop')
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
grid on
end

更多回答(1 个)

Chunru
Chunru 2024-3-6
Try the following
figure;
an = annotation("textbox", "Color", "r", "BackgroundColor", "g", "String", "Text Box");
for i=0:0.01:0.9
an.Position= [i, 0.5, 0.1, 0.1];
drawnow
end
  4 个评论
Charlie
Charlie 2024-3-6
I'll just send the whole code
%% Header
% Written by C. Vogen
% Date Written:3/3/2024
% Modified: None
%% Variable Glossery
% t_start = 0; % seconds
% t_end = 10; % seconds
% x_final = 10; % meters
% y_final = 0; % meters
% g = 9.81 % m/s^2
% m = 150;
%
%% Code Statements
clear; clc; close all;
t_start = 0; % seconds
t_end = 10; % seconds
x_final = 10; % meters
y_final = 0; % meters
g = 9.81; % m/s^2
v_x0 = x_final/t_end; % finding initial x velocity
v_y0 = (y_final + (g*0.5*(t_end^2)))/t_end; % finding initial y velocity
m = 11;
T = zeros(m,2);
incrv = (t_end - t_start)/(m-1);
startCannon =[0,0]; endCannon = [.175,10]; % starts the cannon
Cannon = [startCannon;endCannon];
figure
plot(Cannon(:,1),Cannon(:,2),'LineWidth',5)
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
hold on
pause(1)
flag_text = text(0, 0, '', 'HorizontalAlignment', 'left','VerticalAlignment','bottom', 'FontWeight', 'bold');
Color_start = [.25 .25 .25];
Color_end = [1 1 0];
Color_incv = (Color_end - Color_start)/(m-1);
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2);
for i = 1:m
t = t_start +(i-1)*incrv;
x = v_x0 * t;
y = (v_y0 * t) - ((g * t^2)/2);
Color = Color_start +(i-1)*Color_incv;
pause(0.4)
if i>1
delete(ball) % removing the previous marker
delete(line) % removes the line connecting the box to the point
end
ball = plot(x,y,'o','MarkerFaceColor','r'); % plotting inside the loop
set(ball, 'XData',x, 'YData',y)
set(flag_text, 'Position', [x+0.2, 30], 'String', {['X: ', num2str(x)], ['Y: ', num2str(y)],['I: ', num2str(i)],['T: ',num2str(t)]})
set(square, 'Position', [x, 25, 2.5, 38]);
line = plot([x,x],[0,30],'b','LineWidth',2);
axis normal
title('Visual Loop')
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
grid on
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by