Scatter plot - different colour marker for night and day
2 views (last 30 days)
Show older comments
Hi,
I have hourly radiation data for the whole year (8760 values in total) have used scatter plots to plot this data. My time data is in 'dd-mm-yyyy HH:MM:SS' format and I used startdate/enddate and linspace for this. I was hoping to colour the markers by the time of day e.g. red for 0600-1800 and blue for 1800-0600 but have no idea how to do this as am relatively new to Matlab.
Does anyone know how to do this?
Thanks
1 Comment
Ali
on 29 Oct 2017
if true
--------------------------------------------------- code start
This is an example for your case
Input is "Input_Data", two dimension matrix
Marker_Counter=1;
figure6=figure;
Markers = {'+','o','*','x','v','d','^','s','>','<'};
for i=1:10:size(Input_Data,1)
TPR=Input_Data(i:i+9,7);
FPR=Input_Data(i:i+9,8);
plot(FPR,TPR,strcat('-',Markers{Marker_Counter}));
Marker_Counter=Marker_Counter+1;
hold on
end
plot([0.5 1],[0.5 1],'--');
legend('Minpts = 100','Minpts = 200','Minpts = 300','Minpts = 400','Minpts = 500','Minpts = 600','Minpts = 700','Minpts = 800','Minpts = 900','Minpts = 1000','','Location','SouthEast');
xlabel('FPR or (1-Specificity)','FontSize',12,'FontWeight','bold'); ylabel('TPR or Spensitivity)','FontSize',12,'FontWeight','bold');
title('ROC Space');
close(gcf);
-------------------------------------------- code end
end
--------------------------------------- picture link preview
Answers (1)
dpb
on 19 Feb 2017
Edited: dpb
on 20 Feb 2017
Presuming you converted to datetime array for the times, then
Well, here, let's just build a sample...
>> t=datetime(2001,1,1,[0:365*24].',0,0); t(end)=[]; % 8760-long hourly datetime series
>> iday=iswithin(hour(t),6,18)+1; % vector of day/night indices 1==>night, 2-->day
>> [min(iday) max(iday)] % show the above is so...
ans =
1 2
>> c=[[0 0 1];[1 0 0]]; % colors blue and red as members of 2-element array
>> clr=c(iday,:);
>> whos clr
Name Size Bytes Class Attributes
clr 8760x3 70080 double
>>
Now just pass the clr array to scatter and voila! you'll get the two colors.
What would be kinda' cute here would be to find a sunrise/sunset calculator and tie the colors to it instead of just fixed hours.
scatter only takes a vector of RGB triplets; a thread came up just a week ago or so where I pointed out it would be useful to be able to use the named colors in same way. A TMW employee agreed would be worthy of enhancement request; don't know if he picked it up or not and did so; I'd done it years and years ago.
Oh, almost forgot -- iswithin is a utility routine of mine; just "syntactic sugar" for the double-comparison. It's just
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
one can always just write the conditional expression, but it can get very messy in indexing or other more complex expressions so the factorization often makes for much cleaner-looking user code.
ADDENDUM
I just remembered there's a new TMW function isbetween that does same thing for datetime values. Oh, but hour returns a double array and it can't handle them and doesn't seem to have options for the intervals of other than dates. So, the above seems the route; doesn't look like the datetime class does anything else for you here.
0 Comments
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!