How to make previous inputs progressively fade out in a Matlab plot when I add new inputs
up vote
15
down vote
favorite
Let's say I have this very simple loop
for i=1:10
[xO, yO, xA, yA, xB, yB, xC, yC] = DoSomething(i);
line([xO,xA,xB,xC],[yO,yA,yB,yC]);
pause(0.1);
end
The coordinates that I am plotting correspond to the joints of a multibody system, and I am simulating their positions over time (please see a sample of the plot here):
Since some of the links move in a periodic way, it gets confusing to keep track visually of the movement. For this reason, now comes the question: how can I plot the lines in a way that, when a new line is plotted, the previous lines are faded progressively? In other words, so that I have a gradient from the most recently plotted data (most opaque) to the oldest data (increasingly transparent until it completely fades out).
This way when a new line is drawn in the same position as very old data, I will notice that it is a new one.
matlab plot line transparency matlab-figure
add a comment |
up vote
15
down vote
favorite
Let's say I have this very simple loop
for i=1:10
[xO, yO, xA, yA, xB, yB, xC, yC] = DoSomething(i);
line([xO,xA,xB,xC],[yO,yA,yB,yC]);
pause(0.1);
end
The coordinates that I am plotting correspond to the joints of a multibody system, and I am simulating their positions over time (please see a sample of the plot here):
Since some of the links move in a periodic way, it gets confusing to keep track visually of the movement. For this reason, now comes the question: how can I plot the lines in a way that, when a new line is plotted, the previous lines are faded progressively? In other words, so that I have a gradient from the most recently plotted data (most opaque) to the oldest data (increasingly transparent until it completely fades out).
This way when a new line is drawn in the same position as very old data, I will notice that it is a new one.
matlab plot line transparency matlab-figure
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
Sorta, kinda related.
– gnovice
Dec 3 at 22:45
add a comment |
up vote
15
down vote
favorite
up vote
15
down vote
favorite
Let's say I have this very simple loop
for i=1:10
[xO, yO, xA, yA, xB, yB, xC, yC] = DoSomething(i);
line([xO,xA,xB,xC],[yO,yA,yB,yC]);
pause(0.1);
end
The coordinates that I am plotting correspond to the joints of a multibody system, and I am simulating their positions over time (please see a sample of the plot here):
Since some of the links move in a periodic way, it gets confusing to keep track visually of the movement. For this reason, now comes the question: how can I plot the lines in a way that, when a new line is plotted, the previous lines are faded progressively? In other words, so that I have a gradient from the most recently plotted data (most opaque) to the oldest data (increasingly transparent until it completely fades out).
This way when a new line is drawn in the same position as very old data, I will notice that it is a new one.
matlab plot line transparency matlab-figure
Let's say I have this very simple loop
for i=1:10
[xO, yO, xA, yA, xB, yB, xC, yC] = DoSomething(i);
line([xO,xA,xB,xC],[yO,yA,yB,yC]);
pause(0.1);
end
The coordinates that I am plotting correspond to the joints of a multibody system, and I am simulating their positions over time (please see a sample of the plot here):
Since some of the links move in a periodic way, it gets confusing to keep track visually of the movement. For this reason, now comes the question: how can I plot the lines in a way that, when a new line is plotted, the previous lines are faded progressively? In other words, so that I have a gradient from the most recently plotted data (most opaque) to the oldest data (increasingly transparent until it completely fades out).
This way when a new line is drawn in the same position as very old data, I will notice that it is a new one.
matlab plot line transparency matlab-figure
matlab plot line transparency matlab-figure
edited Dec 3 at 11:08
Wolfie
15.3k51744
15.3k51744
asked Dec 3 at 10:28
Mr. Crystal
826
826
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
Sorta, kinda related.
– gnovice
Dec 3 at 22:45
add a comment |
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
Sorta, kinda related.
– gnovice
Dec 3 at 22:45
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
Sorta, kinda related.
– gnovice
Dec 3 at 22:45
Sorta, kinda related.
– gnovice
Dec 3 at 22:45
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
I'm adding a 2nd answer to clearly separate two completely different approaches. My 1st answer uses the undocumented (and as of 2018b, depreciated) transparency option for lines.
This answer offers a different approach for line drawing which has no compatibility issues (these two 'features' could be implemented independently):
- Create a fixed
n
lines and update their position, rather than creating a growing number of lines. - Recolour the lines, fading to white, rather than changing transparency.
Here is the code, see comments for details:
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 100;
% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) );
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2];
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);
% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );
for ii = 1:n
% Shift the lines around so newest is at the start
lines = [ lines(end), lines(1:end-1) ];
% Overwrite x/y data for oldest line to be newest line
set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
% Update all colours
recolour( lines );
% Pause for animation
pause(0.01);
end
Result:
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
add a comment |
up vote
17
down vote
You can do this by modifying the 4th Color
attribute of past lines.
Here's a demo resulting gif, where I faded out 10% of the transparency each frame, so only the most recent 10 lines are visible.
Here is the code, see my comments for details:
% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 10;
% Main plotting loop
for ii = 1:n
% Plot the line
lines(ii) = line( [0,x(ii)], [0,y(ii)] );
% Loop over past lines.
% Note that we only need to go back as far as ii-nFade, earlier lines
% will already by transparent with this method!
for ip = max(1,ii-nFade):ii
% Set the 4th Color attribute value (the alpha) as a percentage
% from the current index. Could do this various ways.
lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
end
% Delay for animation
pause(0.1);
end
You may want to do some plot/memory management if you've got many lines. You can delete transparent lines by adding something like
if lines(ii).Color(4) < 0.01
delete(lines(ii));
end
Within the loop. This way your figure won't have loads of transparent remnants.
Notes:
- I generated the actual gif using
imwrite
in case that's of interest too. - Apparently the 4th Color value 'feature' has been depreciated in R2018b (not sure it was ever officially documented).
Got enough upvotes to motivate making a slightly more fun demo...
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
add a comment |
up vote
14
down vote
Solution for Matlab 2018a or later (or earlier, later than 2012a at least)
Since the fourth color parameter as alpha value is no longer supported in Matlab 2018a (and apparently was never supposed to as Chris Lueno pointed out), here a solution that works in Matlab 2018a using the patchline
function from the file exchange (credits to Brett Shoelson).
% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);
% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;
% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;
for i=1:n_iterations
% your x, y data
[x, y] = doSomething(i);
% create line with transparency using patchline
p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');
% set alpha of line to fraction of previous alpha value
% only do when first line is already plotted
if i > 1
% loop over all the previous created lines up till this iteration
% when it still exists (delete from that index)
for j = delete_from:i-1
% Update the alpha to be a fraction of the previous alpha value
p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;
% delete barely visible lines
if p(j).EdgeAlpha < 0.01 && delete_from > j
delete(p(j));
% exclude deleted line from loop, so edgealpha is not
% called again
delete_from = j;
end
end
end
% pause and behold your mechanism
pause(0.1);
end
I included the deletion of barely visible lines, as suggested by @Wolfie (my own, perhaps less elegant implementation)
And here a demonstration of a quick release mechanism:
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchangepatchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.
– Wolfie
Dec 4 at 9:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53591823%2fhow-to-make-previous-inputs-progressively-fade-out-in-a-matlab-plot-when-i-add-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
I'm adding a 2nd answer to clearly separate two completely different approaches. My 1st answer uses the undocumented (and as of 2018b, depreciated) transparency option for lines.
This answer offers a different approach for line drawing which has no compatibility issues (these two 'features' could be implemented independently):
- Create a fixed
n
lines and update their position, rather than creating a growing number of lines. - Recolour the lines, fading to white, rather than changing transparency.
Here is the code, see comments for details:
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 100;
% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) );
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2];
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);
% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );
for ii = 1:n
% Shift the lines around so newest is at the start
lines = [ lines(end), lines(1:end-1) ];
% Overwrite x/y data for oldest line to be newest line
set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
% Update all colours
recolour( lines );
% Pause for animation
pause(0.01);
end
Result:
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
add a comment |
up vote
5
down vote
accepted
I'm adding a 2nd answer to clearly separate two completely different approaches. My 1st answer uses the undocumented (and as of 2018b, depreciated) transparency option for lines.
This answer offers a different approach for line drawing which has no compatibility issues (these two 'features' could be implemented independently):
- Create a fixed
n
lines and update their position, rather than creating a growing number of lines. - Recolour the lines, fading to white, rather than changing transparency.
Here is the code, see comments for details:
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 100;
% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) );
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2];
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);
% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );
for ii = 1:n
% Shift the lines around so newest is at the start
lines = [ lines(end), lines(1:end-1) ];
% Overwrite x/y data for oldest line to be newest line
set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
% Update all colours
recolour( lines );
% Pause for animation
pause(0.01);
end
Result:
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
I'm adding a 2nd answer to clearly separate two completely different approaches. My 1st answer uses the undocumented (and as of 2018b, depreciated) transparency option for lines.
This answer offers a different approach for line drawing which has no compatibility issues (these two 'features' could be implemented independently):
- Create a fixed
n
lines and update their position, rather than creating a growing number of lines. - Recolour the lines, fading to white, rather than changing transparency.
Here is the code, see comments for details:
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 100;
% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) );
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2];
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);
% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );
for ii = 1:n
% Shift the lines around so newest is at the start
lines = [ lines(end), lines(1:end-1) ];
% Overwrite x/y data for oldest line to be newest line
set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
% Update all colours
recolour( lines );
% Pause for animation
pause(0.01);
end
Result:
I'm adding a 2nd answer to clearly separate two completely different approaches. My 1st answer uses the undocumented (and as of 2018b, depreciated) transparency option for lines.
This answer offers a different approach for line drawing which has no compatibility issues (these two 'features' could be implemented independently):
- Create a fixed
n
lines and update their position, rather than creating a growing number of lines. - Recolour the lines, fading to white, rather than changing transparency.
Here is the code, see comments for details:
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 100;
% Set up some demo values for plotting around a circle
dt = 0.05; a = 0:dt:2*pi+(dt*nFade); n = numel(a); b = a.*4;
[x1,y1] = pol2cart( a, ones(1,n) ); [x2,y2] = pol2cart( b, 0.4*ones(1,n) );
x = [zeros(1,n); x1; x1+x2]; y = [zeros(1,n); y1; y1+y2];
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1.5,1.5]); ylim([-1.5,1.5]);
% Draw all of the lines, initially not showing because NaN vs NaN
lines = arrayfun( @(x)line(NaN,NaN), 1:nFade, 'uni', 0 );
% Set up shorthand for recolouring all the lines
recolour = @(lines) arrayfun( @(x) set( lines{x},'Color',ones(1,3)*(x/nFade) ), 1:nFade );
for ii = 1:n
% Shift the lines around so newest is at the start
lines = [ lines(end), lines(1:end-1) ];
% Overwrite x/y data for oldest line to be newest line
set( lines{1}, 'XData', x(:,ii), 'YData', y(:,ii) );
% Update all colours
recolour( lines );
% Pause for animation
pause(0.01);
end
Result:
edited Dec 4 at 11:00
answered Dec 4 at 10:27
Wolfie
15.3k51744
15.3k51744
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
add a comment |
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
Very nice alternative, though I personally would prefer using transparency. In another (perhaps very specific case) where you maybe want to fade in lines, this would mean that you get a slight interruption in the a 'solid' line since it a 'more white' line is drawn over it. Totally not relevant to the question, but just a thought. Very much depends on the specific mechanism, linewidth, and to what degree you are a nitpicker.
– rinkert
Dec 4 at 10:58
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
@rinkert Agreed, true transparency is king when doable. I didn't do any real performance tests but in making the above, I think this is slower than the existing answers using transparency too.
– Wolfie
Dec 4 at 11:00
1
1
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
Thanks for your answers, Wolfie. Although I like more the transparency approach, I prefer to this solution since it doesn't use depreciated parameters. Awesome!
– Mr. Crystal
Dec 7 at 9:32
add a comment |
up vote
17
down vote
You can do this by modifying the 4th Color
attribute of past lines.
Here's a demo resulting gif, where I faded out 10% of the transparency each frame, so only the most recent 10 lines are visible.
Here is the code, see my comments for details:
% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 10;
% Main plotting loop
for ii = 1:n
% Plot the line
lines(ii) = line( [0,x(ii)], [0,y(ii)] );
% Loop over past lines.
% Note that we only need to go back as far as ii-nFade, earlier lines
% will already by transparent with this method!
for ip = max(1,ii-nFade):ii
% Set the 4th Color attribute value (the alpha) as a percentage
% from the current index. Could do this various ways.
lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
end
% Delay for animation
pause(0.1);
end
You may want to do some plot/memory management if you've got many lines. You can delete transparent lines by adding something like
if lines(ii).Color(4) < 0.01
delete(lines(ii));
end
Within the loop. This way your figure won't have loads of transparent remnants.
Notes:
- I generated the actual gif using
imwrite
in case that's of interest too. - Apparently the 4th Color value 'feature' has been depreciated in R2018b (not sure it was ever officially documented).
Got enough upvotes to motivate making a slightly more fun demo...
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
add a comment |
up vote
17
down vote
You can do this by modifying the 4th Color
attribute of past lines.
Here's a demo resulting gif, where I faded out 10% of the transparency each frame, so only the most recent 10 lines are visible.
Here is the code, see my comments for details:
% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 10;
% Main plotting loop
for ii = 1:n
% Plot the line
lines(ii) = line( [0,x(ii)], [0,y(ii)] );
% Loop over past lines.
% Note that we only need to go back as far as ii-nFade, earlier lines
% will already by transparent with this method!
for ip = max(1,ii-nFade):ii
% Set the 4th Color attribute value (the alpha) as a percentage
% from the current index. Could do this various ways.
lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
end
% Delay for animation
pause(0.1);
end
You may want to do some plot/memory management if you've got many lines. You can delete transparent lines by adding something like
if lines(ii).Color(4) < 0.01
delete(lines(ii));
end
Within the loop. This way your figure won't have loads of transparent remnants.
Notes:
- I generated the actual gif using
imwrite
in case that's of interest too. - Apparently the 4th Color value 'feature' has been depreciated in R2018b (not sure it was ever officially documented).
Got enough upvotes to motivate making a slightly more fun demo...
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
add a comment |
up vote
17
down vote
up vote
17
down vote
You can do this by modifying the 4th Color
attribute of past lines.
Here's a demo resulting gif, where I faded out 10% of the transparency each frame, so only the most recent 10 lines are visible.
Here is the code, see my comments for details:
% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 10;
% Main plotting loop
for ii = 1:n
% Plot the line
lines(ii) = line( [0,x(ii)], [0,y(ii)] );
% Loop over past lines.
% Note that we only need to go back as far as ii-nFade, earlier lines
% will already by transparent with this method!
for ip = max(1,ii-nFade):ii
% Set the 4th Color attribute value (the alpha) as a percentage
% from the current index. Could do this various ways.
lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
end
% Delay for animation
pause(0.1);
end
You may want to do some plot/memory management if you've got many lines. You can delete transparent lines by adding something like
if lines(ii).Color(4) < 0.01
delete(lines(ii));
end
Within the loop. This way your figure won't have loads of transparent remnants.
Notes:
- I generated the actual gif using
imwrite
in case that's of interest too. - Apparently the 4th Color value 'feature' has been depreciated in R2018b (not sure it was ever officially documented).
Got enough upvotes to motivate making a slightly more fun demo...
You can do this by modifying the 4th Color
attribute of past lines.
Here's a demo resulting gif, where I faded out 10% of the transparency each frame, so only the most recent 10 lines are visible.
Here is the code, see my comments for details:
% Set up some demo values for plotting around a circle
a = 0:0.1:2*pi; n = numel(a);
[x,y] = pol2cart( a, ones(1,n) );
% Initialise the figure, set up axes etc
f = figure(1); clf; xlim([-1,1]); ylim([-1,1]);
% Array of graphics objects to store the lines. Could use a cell array.
lines = gobjects( 1, n );
% "Buffer" size, number of historic lines to keep, and governs the
% corresponding fade increments.
nFade = 10;
% Main plotting loop
for ii = 1:n
% Plot the line
lines(ii) = line( [0,x(ii)], [0,y(ii)] );
% Loop over past lines.
% Note that we only need to go back as far as ii-nFade, earlier lines
% will already by transparent with this method!
for ip = max(1,ii-nFade):ii
% Set the 4th Color attribute value (the alpha) as a percentage
% from the current index. Could do this various ways.
lines(ip).Color(4) = max( 0, 1 - (ii-ip)/nFade );
end
% Delay for animation
pause(0.1);
end
You may want to do some plot/memory management if you've got many lines. You can delete transparent lines by adding something like
if lines(ii).Color(4) < 0.01
delete(lines(ii));
end
Within the loop. This way your figure won't have loads of transparent remnants.
Notes:
- I generated the actual gif using
imwrite
in case that's of interest too. - Apparently the 4th Color value 'feature' has been depreciated in R2018b (not sure it was ever officially documented).
Got enough upvotes to motivate making a slightly more fun demo...
edited Dec 3 at 16:53
answered Dec 3 at 11:01
Wolfie
15.3k51744
15.3k51744
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
add a comment |
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
It seems that line objects are not supposed to support transparency: mathworks.com/help/matlab/ref/alpha.html#buvaucs-1-obj
– Cris Luengo
Dec 3 at 19:20
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
@Cris ugh :/ know a good alternative?
– Wolfie
Dec 3 at 19:44
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
No, I don't. I don't understand why they think only patches need transparency, it certainly is useful for lines as well (had a colleague in the past who used it effectively with parallel coordinates, for example). In this case it doesn't really need to be transparent, you could instead change the RGB values to approach white.
– Cris Luengo
Dec 3 at 19:48
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
@Cris transparency has advantages over colour fading when things overlap for example... Brice's answer suggested colour fading but he deleted it on my suggestion transparency was simpler (before realising it's no longer supported)
– Wolfie
Dec 3 at 19:58
add a comment |
up vote
14
down vote
Solution for Matlab 2018a or later (or earlier, later than 2012a at least)
Since the fourth color parameter as alpha value is no longer supported in Matlab 2018a (and apparently was never supposed to as Chris Lueno pointed out), here a solution that works in Matlab 2018a using the patchline
function from the file exchange (credits to Brett Shoelson).
% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);
% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;
% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;
for i=1:n_iterations
% your x, y data
[x, y] = doSomething(i);
% create line with transparency using patchline
p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');
% set alpha of line to fraction of previous alpha value
% only do when first line is already plotted
if i > 1
% loop over all the previous created lines up till this iteration
% when it still exists (delete from that index)
for j = delete_from:i-1
% Update the alpha to be a fraction of the previous alpha value
p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;
% delete barely visible lines
if p(j).EdgeAlpha < 0.01 && delete_from > j
delete(p(j));
% exclude deleted line from loop, so edgealpha is not
% called again
delete_from = j;
end
end
end
% pause and behold your mechanism
pause(0.1);
end
I included the deletion of barely visible lines, as suggested by @Wolfie (my own, perhaps less elegant implementation)
And here a demonstration of a quick release mechanism:
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchangepatchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.
– Wolfie
Dec 4 at 9:41
add a comment |
up vote
14
down vote
Solution for Matlab 2018a or later (or earlier, later than 2012a at least)
Since the fourth color parameter as alpha value is no longer supported in Matlab 2018a (and apparently was never supposed to as Chris Lueno pointed out), here a solution that works in Matlab 2018a using the patchline
function from the file exchange (credits to Brett Shoelson).
% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);
% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;
% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;
for i=1:n_iterations
% your x, y data
[x, y] = doSomething(i);
% create line with transparency using patchline
p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');
% set alpha of line to fraction of previous alpha value
% only do when first line is already plotted
if i > 1
% loop over all the previous created lines up till this iteration
% when it still exists (delete from that index)
for j = delete_from:i-1
% Update the alpha to be a fraction of the previous alpha value
p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;
% delete barely visible lines
if p(j).EdgeAlpha < 0.01 && delete_from > j
delete(p(j));
% exclude deleted line from loop, so edgealpha is not
% called again
delete_from = j;
end
end
end
% pause and behold your mechanism
pause(0.1);
end
I included the deletion of barely visible lines, as suggested by @Wolfie (my own, perhaps less elegant implementation)
And here a demonstration of a quick release mechanism:
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchangepatchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.
– Wolfie
Dec 4 at 9:41
add a comment |
up vote
14
down vote
up vote
14
down vote
Solution for Matlab 2018a or later (or earlier, later than 2012a at least)
Since the fourth color parameter as alpha value is no longer supported in Matlab 2018a (and apparently was never supposed to as Chris Lueno pointed out), here a solution that works in Matlab 2018a using the patchline
function from the file exchange (credits to Brett Shoelson).
% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);
% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;
% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;
for i=1:n_iterations
% your x, y data
[x, y] = doSomething(i);
% create line with transparency using patchline
p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');
% set alpha of line to fraction of previous alpha value
% only do when first line is already plotted
if i > 1
% loop over all the previous created lines up till this iteration
% when it still exists (delete from that index)
for j = delete_from:i-1
% Update the alpha to be a fraction of the previous alpha value
p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;
% delete barely visible lines
if p(j).EdgeAlpha < 0.01 && delete_from > j
delete(p(j));
% exclude deleted line from loop, so edgealpha is not
% called again
delete_from = j;
end
end
end
% pause and behold your mechanism
pause(0.1);
end
I included the deletion of barely visible lines, as suggested by @Wolfie (my own, perhaps less elegant implementation)
And here a demonstration of a quick release mechanism:
Solution for Matlab 2018a or later (or earlier, later than 2012a at least)
Since the fourth color parameter as alpha value is no longer supported in Matlab 2018a (and apparently was never supposed to as Chris Lueno pointed out), here a solution that works in Matlab 2018a using the patchline
function from the file exchange (credits to Brett Shoelson).
% init the figure
figure(); axes();
hold on; xlim([-1 0.5]); ylim([0 1]);
% set fraction of alpha value to take
alpha_fraction = 0.7;
n_iterations = 200;
% looping variable to prevent deleting and calling already deleted lines
% i.e. to keep track of which lines are already deleted
delete_from = 1;
for i=1:n_iterations
% your x, y data
[x, y] = doSomething(i);
% create line with transparency using patchline
p(i) = patchline(x,y, 'linewidth', 1, 'edgecolor', 'k');
% set alpha of line to fraction of previous alpha value
% only do when first line is already plotted
if i > 1
% loop over all the previous created lines up till this iteration
% when it still exists (delete from that index)
for j = delete_from:i-1
% Update the alpha to be a fraction of the previous alpha value
p(j).EdgeAlpha = p(j).EdgeAlpha*alpha_fraction;
% delete barely visible lines
if p(j).EdgeAlpha < 0.01 && delete_from > j
delete(p(j));
% exclude deleted line from loop, so edgealpha is not
% called again
delete_from = j;
end
end
end
% pause and behold your mechanism
pause(0.1);
end
I included the deletion of barely visible lines, as suggested by @Wolfie (my own, perhaps less elegant implementation)
And here a demonstration of a quick release mechanism:
edited Dec 4 at 9:41
answered Dec 3 at 11:02
rinkert
1,367417
1,367417
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchangepatchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.
– Wolfie
Dec 4 at 9:41
add a comment |
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchangepatchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.
– Wolfie
Dec 4 at 9:41
1
1
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchange
patchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.– Wolfie
Dec 4 at 9:41
Nice solution for newer MATLAB versions, annoying how MathWorks removed the alpha functionality, leaving us the FileExchange
patchline
and not a simple native replacement! Thanks for expanding on this alternative in your edit.– Wolfie
Dec 4 at 9:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53591823%2fhow-to-make-previous-inputs-progressively-fade-out-in-a-matlab-plot-when-i-add-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What version of MATLAB are you using?
– Wolfie
Dec 3 at 12:05
Sorta, kinda related.
– gnovice
Dec 3 at 22:45