How to plot multiple time series cell arrays as a shadowed area

Hi guys!
I have the following cell data (each column represents a different algorithm for comparison; each row represents a experiment run):
* The data is attached.
Using this code:
figure
hold on
cellfun(@plot,mutation1)
I get this plot:
Question 1: How can I represent each column of data in a specific color in the plot using the code above?
Three columns have constant data (single lines: purple, blue and green). Two columns have data with small variations (the other two multicolored lines on the graph).
Question 2: Would it be possible to represent these two multicolored lines (which are made up of a series of other lines) as a shaded area with a middle line?
Thank you very much!

 Accepted Answer

Answer 1: See the colororder call.
Answer 2: Yes, although it takes a bit of exploring to determine what those curves are. I left in my ‘exploration’ steps (commented-out). After that, this is straightforward.
Try this —
LD = load('mutation1.mat');
mutation1 = LD.mutation1
mutation1 = 20×5 cell array
{300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double} {300×1 double}
colororder(turbo(numel(mutation1))) % Use The 'turbo' 'colormap' To Define The Colours
mutationmtx = cell2mat(reshape(mutation1, 1,[]));
% figure
% surf(mutationmtx, 'EdgeColor','none')
% colormap(turbo)
% xlabel('Columns')
% ylabel('Rows')
%
% figure
% plot((1:size(mutationmtx,2)), mutationmtx(1,:))
% hold on
% plot((1:size(mutationmtx,2)), mutationmtx(150,:))
% hold off
% grid
% legend('1','150', 'Location','best')
Lv1 = mutationmtx(1,:) > 0.3;
Lv2 = mutationmtx(150,:) > 0.05;
highest = max(mutationmtx(:,Lv1 & ~Lv2),[],2);
lowest = min(mutationmtx(:,Lv1 & Lv2), [],2);
middleline = median([highest lowest],2);
% figure
% plot(highest, 'g')
% hold on
% plot(lowest, 'r')
% hold off
v = (1:numel(highest)).';
figure
hold on
cellfun(@plot,mutation1)
patch([v; flip(v)], [highest; flip(lowest)], [1 1 1]*0.75, 'FaceAlpha',0.5)
plot(middleline, '-g', 'LineWidth',1.5)
% Ax = gca;
% Ax.XScale = 'log';
See the documentation on patch for those details.
EDIT — (19 Sep 2023 at 13:52)
Forgot about ‘middle line’, Now added.
.

4 Comments

Thank you very much @Star Strider!
I changed your code a little to do exactly what I needed:
load mutation1.mat
colororder(turbo(numel(mutation1))); close;
mutationmtx = cell2mat(reshape(mutation1,1,[]));
min_ga4 = min(mutationmtx(:,61:80),[],2);
max_ga4 = max(mutationmtx(:,61:80),[],2);
min_ga5 = min(mutationmtx(:,81:100),[],2);
max_ga5 = max(mutationmtx(:,81:100),[],2);
v = (1:numel(min_ga4)).';
figure
hold on
cellfun(@plot,mutation1)
patch([v; flip(v)], [max_ga4; flip(min_ga4)], 'g', 'FaceAlpha',0.5)
patch([v; flip(v)], [max_ga5; flip(min_ga5)], 'r', 'FaceAlpha',0.5)
legend('Line1','Line2','Line3','Line4','Line5')
But the Legend is not being displayed correctly...
And I couldn't use the colororder function correctly. Is it possible to have a legend for each line and each shaded area?
As always, my pleasure!
I was not quite certain what you wanted. I am happy that you could use my code to get your desired result.
There are 100 lines, so the legend would look busy. You might be able to accommodate that by using the 'NumColumns' name-value pair, however it would still be crowded. The best way to be certain everything was assigned values in the legend would be something like this —
load mutation1.mat
mutationmtx = cell2mat(reshape(mutation1,1,[]));
min_ga4 = min(mutationmtx(:,61:80),[],2);
max_ga4 = max(mutationmtx(:,61:80),[],2);
min_ga5 = min(mutationmtx(:,81:100),[],2);
max_ga5 = max(mutationmtx(:,81:100),[],2);
v = (1:numel(min_ga4)).';
lgdc = compose('Line %3d',1:size(mutationmtx,2));
colororder(turbo(size(mutationmtx,2)));
cm = colormap(turbo(size(mutationmtx,2)));
figure
hold on
for k = 1:size(mutationmtx,2)
hcp(k,:) = plot(mutationmtx(:,k), 'DisplayName',lgdc{k}, 'Color',cm(k,:));
end
hp1 = patch([v; flip(v)], [max_ga4; flip(min_ga4)], 'g', 'FaceAlpha',0.5, 'DisplayName','ga4');
hp2 = patch([v; flip(v)], [max_ga5; flip(min_ga5)], 'r', 'FaceAlpha',0.5, 'DisplayName','ga5');
% for k = 1:numel(hcp)
% hcp(k).DisplayName = lgdc{k};
% end
legend([hcp; hp1; hp2], 'Location','southoutside', 'NumColumns',6, 'FontSize',5)
I have no idea why colororder wasn’t working correctly, since it even initially failed with the loop. I changed the code to plot the ‘mutationmtx’ columns and associated colours individually, along with their 'DisplayName' properties, since that made the legend easier to work with.
The legend is going to be a problem with 102 entries. See its documentation for more options.
.
Thank you again @Star Strider! I found a workaround:
Since the first three columns of the cell have the same values (they are deterministic), it is not necessary to plot all of them, so I took the average (I could also take just one sample from each of the three columns)...
For columns 4 and 5 (they are stochastic) I took the maximum and minimum values (because these are variables).
So I didn't need to use the cellfun function and I was able to define the colors and create the correct caption.
load mutation1.mat
n = size(mutation1,1);
mutationmtx = cell2mat(reshape(mutation1,1,[]));
ga1 = mean(mutationmtx(:,1:n),2);
ga2 = mean(mutationmtx(:,n+1:2*n),2);
ga3 = mean(mutationmtx(:,2*n+1:3*n),2);
min_ga4 = min(mutationmtx(:,3*n+1:4*n),[],2);
max_ga4 = max(mutationmtx(:,3*n+1:4*n),[],2);
min_ga5 = min(mutationmtx(:,4*n+1:5*n),[],2);
max_ga5 = max(mutationmtx(:,4*n+1:5*n),[],2);
v = (1:numel(min_ga4)).';
figure
hold on
plot(v,ga1,'Color',[0.0000,0.4470,0.7410]')
plot(v,ga2,'Color',[0.9290,0.6940,0.1250]')
plot(v,ga3,'Color',[0.4940,0.1840,0.5560]')
patch([v; flip(v)], [max_ga4; flip(min_ga4)], 'g', 'FaceAlpha',0.5)
patch([v; flip(v)], [max_ga5; flip(min_ga5)], 'r', 'FaceAlpha',0.5)
legend('GA1','GA2','GA3','GA4','GA5')
The result:
I'm sorry if I hadn't explained it clearly from the beginning. As English is not my native language, it is sometimes difficult to correctly express my ideas. =)
As always, my pleasure!
No worries — some concepts are difficult to put into words regardless of the language, especially some mathematical concepts. I am happy that I could help you get this sorted.
.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!