How to dim line on plot?
18 次查看(过去 30 天)
显示 更早的评论
I have the following code which animates a particle. When the particle is about to run off the plot's window, the code sends the particle back into the plot window on the opposite side. This ends up creating a bunch of lines across the window. I don't want to entirely get rid of any of the lines, but I do want to find a way to dim the existing lines after the particle reenters the window on the opposite side. So once the particle reenters, the existing lines either dim or they could change color. Is this possible?
clear;
clc;
numMoves = 500;
x_List = zeros(numMoves,1);
y_List = zeros(numMoves,1);
th_List = zeros(numMoves,1);
xstep = 1.9;
ystep = 0.9;
xdiff = 0.0;
ydiff = 0.0;
% Make fig. and start the loop:
figure;
hold;
for t = 2:numMoves
% each iteration, add a rand val between -1 and 1,
% and save new x & y coordinates
th_list(t) = th_List(t-1) + 1*(rand()-0.5);
x_List(t) = x_List(t-1) + xdiff*2*(rand()-0.5) + xstep*cos(th_list(t));
y_List(t) = y_List(t-1) + ydiff*2*(rand()-0.5) + ystep*sin(th_list(t));
x_List(t) = x_List(t-1) + xdiff*2*(rand()-0.5) + xstep*cos(th_list(t));
y_List(t) = y_List(t-1) + ydiff*2*(rand()-0.5) + ystep*sin(th_list(t));
% animate the particle:
cla
plot(x_List(1:t),y_List(1:t),'-r')
plot(x_List(1:t),y_List(1:t),'-k')
quiver (x_List(t), y_List(t), cos(th_list(t)), sin(th_list(t)),50)
quiver (x_List(t), y_List(t), cos(th_list(t)), sin(th_list(t)),50)
if x_List(t) > 500
x_List(t) = x_List(t) - 1000;
elseif x_List(t) < -500
x_List(t) = x_List(t) + 1000;
elseif y_List (t) > 500
y_List(t) = y_List(t) - 1000;
elseif y_List(t) < -500
y_List(t) = y_List(t) + 1000;
end
axis([-500 500 -500 500]) %this sets the plot size
pause(0.03)
end
0 个评论
回答(2 个)
Star Strider
2016-12-13
It is difficult to follow your code even when I run it. The ‘line’ properties do not include an ‘alpha’ (transparency) property, so I would define a different colour for the second plot. Here, I define the colour as a light gray.
Example:
x = linspace(0,2*pi,5000);
y1 = sin(5*x);
y2 = cos(5*x);
gray = [0.7 0.7 0.7];
figure(10)
plot(x, y1, 'Color',gray)
hold on
plot(x,y2,'-k')
hold off
grid
1 个评论
Star Strider
2023-8-1
There is a line transparency property and to set it, add it as the fourth element of the 'Color' property —
x = 0:0.01:1;
y = sin(2*pi*x);
figure
hp = plot(x, y, 'LineWidth',3);
hp.Color = [1 0 1];
grid
title('Original')
figure
hp = plot(x, y, 'LineWidth',3);
hp.Color = [1 0 1 0.25];
grid
title('Original With \alpha = 0.25')
I learned about it (an undocumented property) several years after posting this.
.
Cyriaque Guillot
2023-8-1
编辑:Voss
2023-8-1
You can actually change the alpha parameter of the line. You need to create a [1x4] matrix that contains the parameters [red, green, blue, alpha] all between 0 and 1. If I retrieve Star Strider's code, I would have:
clear, close all
clc
x = linspace(0,2*pi,5000);
y1 = sin(5*x);
y2 = cos(5*x);
gray = [0.7, 0.7, 0.7, 0.75];
figure(10)
plot(x, y1, 'Color',gray)
hold on
plot(x,y2,'-k')
hold off
grid
In that case, there is only 75% of the light that will be ploted for the first line.
Hope it'll help some of you
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Vector Fields 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


