How to manipulate colors in plot for array?

2 views (last 30 days)
Joy Shen
Joy Shen on 16 Jul 2023
Edited: dpb on 16 Jul 2023
If you run this code, you'll see that I have four curves. I want to make the Ds_1 curves the same color, and the Ds_2 curves a different but same color. I also want to have all the "Acceptable" curves to be dashed '--' and the "Degraded" curves to be starred '*'. But I have an array that iterates through Fx. How can I go about changing the colors and outlines?
%% Fragility functions
% Define degradation states (existing damage states prior to the flood)
% Creating a cell array called DegState DegState = {'Acceptable', 'Degraded', 'Failure-Missing'}
% Define baseline capacity values for each damage state for non-degraded seal
BaselineHCLPF(DmS1)= NaN; % capacity against no damage or more damage
Unrecognized function or variable 'DmS1'.
BaselineHCLPF(DmS2)=0.50*25; % capacity against moderate/partial or more damage
BaselineHCLPF(DmS3)=25; % capacity against complete failure
%% Calculate all fragility parameters for Lognormal Distribution
zeta=0.3; % assumed value (composite standard distribution)
pstar=0.1; % non-exceedance probability for assumed component capacity HCLPF
% FIX, for some reason it only works if you run this twice???
for iDG=1:length(DegState)
for iDM=1:length(DamState)
if iDM==DmS1%|| iDG==DgS3 %if damage state is in the DmS1, the lamda is almost 0 (cannot be zero bc lognormal)
lam(iDM,iDG)=1e-17; %artificial -> probability will be 1.0 later
else
lam(iDM,iDG)=log(BaselineHCLPF(iDM)*DegCapRed(iDG))-zeta*norminv(pstar);
end
end
end
% Define probability objects for each damage state/degradation combination
for iDG=1:length(DegState)
for iDM=1:length(DamState)
pd_frag{iDG}{iDM}=makedist('Lognormal',lam(iDM,iDG),zeta); % Creating a 1xlength(DegState) cell, within each cell is another 1xlength(DamState)
end
end
% Plot fragility PMFs
figure; legct=0;
for iDG=1:2%length(DegState)
for iDM=2:length(DamState)
x=[]; x=linspace(0,20,100000);
Fx=[]; Fx=cdf(pd_frag{iDG}{iDM},x);
plot(x,Fx); hold on
legct=legct+1; leg{legct}=strcat(DegState{iDG},'; ',DamState{iDM});
end
end
title('Family of Fragility Functions for a Penetration Seal','FontSize',15)
%subtitle('Assuming Lognormal Distribution')
xlabel('\eta_1 (ft)','FontSize',15)
ylabel('P(DS\geqds_i|\eta_1=n)','FontSize',15)

Answers (2)

Voss
Voss on 16 Jul 2023

dpb
dpb on 16 Jul 2023
Edited: dpb on 16 Jul 2023
"If you run this code, ..."
Of course, we can't run this code because we don't have the data needed...but, to solve your problem raised doesn't need that..the following should give you the general idea, salt to suit...
C=["r","b"]; L=["-","--"]; M=["","*"]; % lookup tables for color, linestyle, marker
figure, subplot(2,1,1), hold on
for ii=1:2,for jj=1:2
LS=C(ii)+L(jj)+M(jj); % pick line, color, marker by indices
plot(randn(10,1),LS)
end,end
title('Sequence [a b b]')
subplot(2,1,2), hold on
for ii=1:2,for jj=1:2
LS=C(ii)+L(ii)+M(jj); % pick line, color, marker by indices
plot(randn(10,1),LS)
end,end
title('Sequence [a a b]')
As can see, you can change the patterns to match as desired by picking proper combinations of indices and order in the lookup arrays.
You'll use your indices and choices to match wants, of course...
  3 Comments
dpb
dpb on 16 Jul 2023
%% Fragility functions
% Define degradation states (existing damage states prior to the flood)
% Creating a cell array called DegState DegState = {'Acceptable', 'Degraded', 'Failure-Missing'}
% Define baseline capacity values for each damage state for non-degraded seal
BaselineHCLPF(DmS1)= NaN; % capacity against no damage or more damage
Unrecognized function or variable 'DmS1'.
just for starters....
The lookup code works as shown; you just have to use it with the correct indices for your case -- it's not easy to parse the nested cell addressing you have nor is it clear just how many there are other than the first description seems to imply two levels of each factor -- but ascertainig which is which by deciphering code intent is more work than have time to devote.
In particular, how many levels of DamState are there? If more than two but only want two linestyles, then will have to discretize those into their appropriate bins...
dpb
dpb on 16 Jul 2023
Edited: dpb on 16 Jul 2023
"...something like LS(ii,jj)"
No. LS is a single string built from the desired combination of indices into the three lookup tables, each of which is 1D vector.
If you can't define the pattern from those by the loop indices, then you would need to build a 2D lookup table or just identify them directly. It should be possible if the labelling is consistent with the loop indexing levels to just use the 1D approach above, however.
Also realize you're not limited to using just the indices for the lookup; again the sample code is just that; an illustration of using lookup tables; it's the simplest form there is just using the numeric index.
You defined
DegState = {'Acceptable', 'Degraded', 'Failure-Missing'}
There's nothing keeping you from having three levels(*) if those are all used and then using either the index variable in the loop over the states or even matching which level it is as the index into the lookup array, same as you get the text label to match.
(*) And, there's nothing that says all those levels have to be unique; {'Acceptable'} could be green and both others black -- and there's nothing that says that the marker/linestyle can't be unique or match. It's all up to you; all we have illustrated here is the tools...

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!