Plot straight lines between the data points in the same colour as the data points with lots of NaN values

2 次查看(过去 30 天)
As my code is working now, I've integrated the solution in my code ("working solution") but left the original version ("old - not working")
Original Question:
I'm using Matlab R2019a. Apart from what is working so far (see code below), I would like to:
  • plot straight lines between the data points of the same site in chronological order in the same colour as the data points (not easy because of the NaN values)
  • don't loose the legend with all the sites, even if they are only NaN values for that site. (For better comparison with plots for other Chemicals)
I've data from different locations. Each "Location" has a different number of measuring "sites". These sites can be of three different types (GWM, VZ, Rain)
The data for one location is stored in a matrix.
I've created a function to be able to use the plot for Concentrations of different Chemicals. The matrix contains a lot of NaN.
set(groot, 'DefaultLineMarkerEdgeColor', 'k')
Colour=flip(colorcube(8),1); Colour=Colour(2:8,:);
Chemical = 'Nitrate';
% create random Concentration matirx with NaN values
ConcMatrix=rand(37,11); x1 = logical(randi([0, 1], [37,11])); ConcMatrix(x1==1) = [NaN];
x2 = logical(randi([0, 1], [37,11])); ConcMatrix(x2==1) = [NaN]; x3 = logical(randi([0, 1], [37,11])); ConcMatrix(x3==1) = [NaN]; ConcMatrix(:,6:7) = NaN;
%ConcMatrix = fillmissing(ConcMatrix, 'linear');
% create random dates
NUMBER_RANDOM_TIMES = 37; SECONDS_PER_DAY = 24*60*60;
START_DATE = '2010-01-01'; END_DATE = '2020-01-30';
startDateNum = datenum(START_DATE,'yyyy-mm-dd'); endDateNum = datenum(END_DATE, 'yyyy-mm-dd');
dayRange = endDateNum - startDateNum; secondsRange = SECONDS_PER_DAY*dayRange; randomNumberOfSeconds = randi(secondsRange,NUMBER_RANDOM_TIMES,1);
randomDatenums = startDateNum + randomNumberOfSeconds/SECONDS_PER_DAY;
date= datetime(datestr(randomDatenums));
% Plot
figure
Location = "Location A"; nrGWM = 6; nrVZ = 5; nrRain = 0; nrSites = nrGWM + nrVZ + nrRain;
ConcPlot(date,Location,Chemical,ConcMatrix,nrGWM,nrVZ,nrRain,Colour)
title(sprintf("%s",Location));
% Concentration Plot-Function
function ConcPlot(date,Location,Chemical,ConcMatrix,nrGWM,nrVZ,nrRain,Colour)
nrSites = nrGWM + nrVZ + nrRain;
for i=1:nrSites
if i <= nrGWM
% working solution:
ind = ~isnan(ConcMatrix(:,i));
plot(date(ind),ConcMatrix(ind,i),'-s','MarkerFaceColor',Colour(i,:),'Color',Colour(i,:))
% old - not working: plot(date,ConcMatrix(:,i),'-s','MarkerFaceColor',Colour(i,:),'Color',Colour(i,:))
Legend{i}=sprintf('GWM %1.0f',i);
hold on
elseif (i > nrGWM) && (i <= nrGWM + nrVZ)
% working solution:
ind = ~isnan(ConcMatrix(:,i));
plot(date(ind),ConcMatrix(ind,i),'-o','MarkerFaceColor',Colour(i-nrGWM,:),'Color',Colour(i-nrGWM,:))
% old - not working: plot(date,ConcMatrix(:,i),'-o','MarkerFaceColor',Colour(i-nrGWM,:),'Color',Colour(i-nrGWM,:))
Legend{i}=sprintf('VZ %1.0f',i-nrGWM);
elseif i > (nrGWM+nrVZ)
plot(date,ConcMatrix(:,i),'v','MarkerFaceColor',Colour(i-nrGWM-nrVZ,:)) %,Colour(i,:))
Legend{i}=sprintf('Rain %1.0f',i-nrGWM-nrVZ);
end
end
title(sprintf("%s Concentration in %s",Chemical,Location))
xlabel("Sampling Date");
ylabel(sprintf("%s concentration [mg/L]",Chemical));
legend(Legend);
ylim([0 1])
end
Thank you for your help, Freya
PS: This is what the plot looks like so far. But what I would like to do is connect the dots of same shape & colour in chronological order. I.e. I want to connect all the red(/green/blue/pink) dots with lines and all the red(/green/blue/pink) square with lines. The lines should start at the first appearing symbol (eg. green square at 2012) and then continue to the next symbol of the same type: so green square between 2012 & 2013.
  11 个评论
Freya
Freya 2020-2-19
@Star Strider: I'm sorry for that. I couldn't make it work with your solution.. I might have applied it at the wrong place of my code. I think I got confused by the indices. I'm sorry!

请先登录,再进行评论。

采纳的回答

darova
darova 2020-2-18
Remove NaN?
ind = ~isnan(y);
plot(x(ind),y(ind),'or')
Remember about accepting the answer if it helps

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by