MATLAB Answers

How can I shade confidence intervals and assign colors to frequency spectra plot (pwelch)

19 views (last 30 days)
Heidi Hirsh
Heidi Hirsh on 9 Dec 2019
Edited: Jeremy Marcyoniak on 9 Dec 2019
I am trying to plot power spectra for timeseries data with 95% confidence intervals. Right now I have plotted 2-3 depths of data in each spectra plot and I can plot the upper and lower confidence bounds as dashed lines. I am unsure how to the control the color of my spectra lines as well as the confidence intervals. And ideally I would like to shade the confidence interval instead of using lines. I have just made a mess so far (see attachment) . Any tips would be much appreciated. This is my first time working with this kind of plot/data. Thank you so much!
Here is my code for the plot attached:
note: I would like to ideally keep plotting the spectra in loops in case I want to add other depths (up to 9 different instruments)
_______________________________________
%% Combo PK spectra at NFFT = 8192
close all
f7=figure(7);
sgtitle('PK Frequency Spectra, July 23 - October 4 (NFFT=8192)')
P = .95; %confidence interval
clear nfft; nfft = 8192;
%pH
subplot(2,2,1)
clear jdo; jdo=[2,8];
for j=jdo
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK.PH(j,spgood))),[],[],nfft,288);
h=loglog(F,PxxU,F,PxxC,'--');
hold on
end
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{pHpH} (days)','fontsize',20)
title('pH');
set(gca,'fontsize',16)
xlim([.1 10])
%legend('6.1 mab','1 mab','location','southwest')
%Oxygen
subplot(2,2,2)
clear jdo; jdo=[3 8];
for j=jdo
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK.DO(j,spgood))),[],[],nfft,288);
h=loglog(F,PxxU,F,PxxC,'--');
hold on
end
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{DODO} (days)','fontsize',20)
title('Dissolved Oxygen');
set(gca,'fontsize',16)
xlim([.1 10])
%legend('PK 5.2 mab','PK 1 mab','location','southwest')
%Temperature
subplot(2,2,3)
clear jdo; jdo=[2 8 1];
for j=jdo
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK.TEMP(j,spgood))),[],[],nfft,288);
h=loglog(F,PxxU,F,PxxC,'--');
hold on
end
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{TT} (days)','fontsize',20)
title(['Temperature']);
set(gca,'fontsize',16)
xlim([.1 10])
%legend('Surface','6.1 mab','1 mab','location','southwest')
%Salinity
subplot(2,2,4)
clear jdo; jdo=[2 8];
for j=jdo
clear PxxU F PxxC; [PxxU,F,PxxC] = pwelch(detrend(fixgaps(PK.SAL(j,spgood))),[],[],nfft,288);
h=loglog(F,PxxU,F,PxxC,'--');
hold on
end
grid on
set(gca,'fontsize',20)
xlabel('f (cpd)','fontsize',20)
ylabel('S_{SS} (days)','fontsize',20)
title('Salinity');
set(gca,'fontsize',16)
xlim([.1 10])
%legend('6.1 mab','1 mab','location','southwest')

  0 Comments

Sign in to comment.

Answers (1)

Jeremy Marcyoniak
Jeremy Marcyoniak on 9 Dec 2019
Edited: Jeremy Marcyoniak on 9 Dec 2019
The plot commands accept a 3 element vector in place of the built-in colors, each element being a number between 0 and 1 representing the components of red, green and blue, respectively. So, for example
plot(x,y,'Color',[1 0 0])
plots x vs. y with a red line. You can make an N-by-3 matrix of line colors to use in your loops if you want to avoid re-using colors.

  0 Comments

Sign in to comment.

Sign in to answer this question.