Filter löschen
Filter löschen

make a contour plot with dates on x axis, time on y axis and fog concentration as z values.

35 Ansichten (letzte 30 Tage)
I need to make a contour plot based on the calculated total fog. I am unable to do so as I am new to MATLAB. Hope Someone can help me.For reference I am attaching a sample of my data. Thanks
  4 Kommentare
dpb
dpb am 28 Sep. 2018
Will need to use NaN to infill I think to get a regular grid; I imported the file as table to 'spearmint some.
contour may also require using datenum rather than datetime for the date/time axes; it is one of the specialty plotting routines not yet (R2017b) gotten to with aliased version for the new class.
Toni
Toni am 13 Mär. 2023
For contour plots, x and y axis ticks labels can be modifed via xticklabels and yticklabels. Reshape in the answer below did not work due to missing data, but code below avoids that by inserting Nan where data is missing.
filename = '1st-10th_dec.csv'
data=readtable(filename)
data.Date = datetime(data.Date,'inputformat','dd-MM-yyyy');
data.Time = duration(data.Time,'inputformat','hh:mm');
date = unique(data.Date);
t = unique(data.Time);
for i=1:length(date)
for j=1:length(t)
index = find(data.Date == date(i) & data.Time == t(j));
if ~isempty(index)
v(j,i) = data.Fog___(index);
else
v(j,i) = NaN;
end
end
end
figure
contourf(datenum(date),datenum(t),v)
xticks(datenum(date))
xticklabels(string(date))
t_ticks = [min(t):hours(2):max(t)];
yticks(datenum(t_ticks))
yticklabels(string(t_ticks))

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

jonas
jonas am 28 Sep. 2018
Bearbeitet: jonas am 28 Sep. 2018
As dbp said, contour is not compatible with datetime format. Here's an example with surf, which is quite similar.
data=readtable('1st-10th_dec.csv')
date=datetime(data{:,1},'inputformat','dd-MM-yyyy');
t=duration(data{:,2},'inputformat','hh:mm');
v=data{:,3};
[~,udate]=findgroups(date);
[~,ut]=findgroups(t);
v=reshape(v,numel(ut),numel(udate))
surf(udate,ut,v)
view([0 90])
and here is the example with contour,
data=readtable('1st-10th_dec.csv')
v=data{:,3};
date=nan(size(data,1),1)
t=nan(size(data,1),1)
date(~isnan(v))=datenum(data{~isnan(v),1},'dd-mm-yyyy')
t(~isnan(v))=datenum(data{~isnan(v),2},'HH:MM')
[~,udate]=findgroups(date);
[~,ut]=findgroups(t);
v=reshape(v,numel(ut),numel(udate))
contour(udate,ut,v)
datetick('x','dd/mm','keeplimits','keepticks')
datetick('y','hh:mm','keeplimits','keepticks')
The resulting plot is quite ugly. This is however easy to fix by interpolation.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by