How can you plot multiple, superimposed traces such that where traces overlap, the background trace is faded?

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

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.]
Oh my...sounds like a lot of work! Thanks for your prompt reply, Walter.
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.
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.
All true, I agree. But if there is a simple way to implement, it would be more eye-catching to readers. Whether the added effort will justify the plotting is a decision yet to be made.

Sign in to comment.

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

1 Comment

Thanks for a solution, Nathan I'll try this and get back to you (probably not for a couple of weeks, however).

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 16 May 2025

Commented:

on 16 May 2025

Community Treasure Hunt

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

Start Hunting!