How can you plot multiple, superimposed traces such that where traces overlap, the background trace is faded?
Show older comments
In a recent publication on imaging nerve cell activity there is a figure showing multiple lines of responses where many of the traces overlap:

The traces have been plotted in such a way that large responses "pop" out and are clearly visible. This is because when traces overlap, the background trace is faded. (The foreground trace may also be darkened...I can't really tell). Here is a higher mnagnification of a portion of the above:

MY QUESTION: Is there a way that this effect can be achieved in Matlab?
5 Comments
Walter Roberson
on 16 May 2025
There is no built-in way, and no simple way to do this.
You would have to calculate all of the intersections. In order to get the fading effect, you would either have to plot() seperate lines for the faded area, or else you would have to use patch() [patch can adjust the alpha data along "lines" but plot() is restricted to a single alpha value for the entire line.]
Stephen
on 16 May 2025
DGM
on 16 May 2025
Ooo. This looks like an interesting one. I agree with Walter. Some trickery with a stack of semitransparent patch() objects would be my first thought. The trick would probably be predetermining the required axes extents and closing each patch object outside the drawn plot area.
William Rose
on 16 May 2025
It would be nice to do what you propose, but certainly not necessary or expected. I've read my share of papers with such plots, and I don't think I've seen one where the "background" trace is faded out. The reader can get the idea quite effectively without that nice but hard-to-implement feature.
Good luck with your work.
Stephen
on 16 May 2025
Answers (1)
Hi Stephen,
You can achieve this using 'waterfall' on your data, setting the FaceAlpha property to some fractional number, and manipulating the CData property of the patch (the first column of this is the bottom color!):
swaves = 150*real(ifft(eye(150))); % a fun way to generate cosine waves with integer fratctional periods
wf = waterfall(swaves(1:8, :)); % plot the first 8 modes
view(165, 45); % set the view angle
set(wf, 'FaceAlpha', 0.5, 'EdgeColor', 'flat', 'LineWidth', 1.2);
wf.CData(1, :) = NaN; % this removes the bottom edge
wf.CData(~isnan(wf.CData)) = 1; % set all the color data to index the top of the colormap
colormap(hex2rgb('#7da4c7')); % change the colormap to a single color
grid("off") % grid off to illustrate the lack of bottom edges
zlim([-2 2]) % scale the view angle
Categories
Find more on Graphics Object Programming 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!