Home > Net >  Plotting transparent lines in MATLAB
Plotting transparent lines in MATLAB

Time:06-17

I'm trying to create a plot in MATLAB of ERP data. At present, the below code plots a waveform for 26 participant for channel 1, with mean amplitude on Y axis and time on X. I want to change the plot such that the waveforms are all light grey, but become darker where there are a lot of waves on top of one another.

times = Condition1.time;
channel = [1]; 

% the difference (for every participant, every channel, and every timepoint)
diff = (grandaverages.Condition1.individual) - (grandaverages.Condition2.individual);

channeldiffwaves = squeeze( mean( diff(:,channels,:), 2 ) );

% make the space to put the figure on
figure; hold on;

% list of participants to plot
plotparticipantIdx = [1:26]; 

% go through each participant
for participantIdx = plotparticipantIdx

ptpdiffwave = channeldiffwaves( participantIdx, : );

plot( times, ptpdiffwave )

end

% make a x-axis at zero
plot( times, zeros( size(times) ), 'k' )

enter image description here

How can I make all lines light gray and darkening where lines overlap?

CodePudding user response:

You can use the RGB-alpha quadruplet:

plot(x, y, 'Color', [0 0 0 0.3]);

The first three entries are your standard RGB-triplet, i.e. 0 0 0 for black, then change the alpha (=transparency) between 0 (invisible) and 1 (completely visible).

As a side-note, this isn't even documented in the Chart line appearance and behavior part of MATLAB's documentation.

I can't quite figure out when this syntax was introduced; on my R2007b it doesn't work, but from 2017 onwards this solution starts popping up on the internet, thus I presume this was introduced with the overhaul of the graphic's engine in R2016b.

  • Related