Plot straight lines between the data points in the same colour as the data points with lots of NaN values
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Star Strider
el 18 de Feb. de 2020
@Freya — That was my Answer that I then deleted when you said that it did not work.
Oh, well...
Respuesta aceptada
darova
el 18 de Feb. de 2020
Remove NaN?
ind = ~isnan(y);
plot(x(ind),y(ind),'or')
Remember about accepting the answer if it helps
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!