Hello, All. Thank you in advance for any help, contribution and your time. I have been asked to do a contour plot with a data vs time (date and month), the data is like ID.ST
27 views (last 30 days)
Show older comments
Hello, All. Thank you in advance for any help, contribution and your time. I have been asked to do a contour plot with a data vs time (date and month), the data is like
ID.STN
LON
LAT
START.DATE/
Abala
39.76
13.34
27-Jul
Abobo
34.31
7.53
13-Jul
Abomsa
39.837
8.479
25-May
AddisAbabaBole
38.79871
8.981081
18-May
AddisAbabaObs
38.7475
9.01891
17-May
Adele
39.90292
7.7505
24-May
Adet
37.49312
11.2745
21-May
2 Comments
Aquatris
on 15 Apr 2024 at 8:49
The formatting of the question makes it hard to read.
Also, what type of plot are you looking for? what is your X axis what is your Y axis and what do you want to see in the Z axis(contour lines)?
Answers (1)
Voss
on 15 Apr 2024 at 19:08
Edited: Voss
on 15 Apr 2024 at 19:31
Maybe something like this would work:
% read the file into a cell array
C = readcell('data.txt');
% make that cell array into a nice table
C = reshape(C,4,[]).';
T = cell2table(C(2:end,:),'VariableNames',strrep(C(1,:),'/',''));
T.('START.DATE') = datetime(T.('START.DATE'),'InputFormat','dd-MMM','Format','preserveinput')
% create a figure for plotting
figure
% plot the lon/lat data
plot(T.LON,T.LAT,'.k')
hold on
% make a scatteredInterpolant for constructing the contour
t_min = min(T.('START.DATE'));
z = days(T.('START.DATE')-t_min); % use number of days since t_min in contour Z
SI = scatteredInterpolant(T.LON,T.LAT,z);
% define the grid over which contour Z is calculated
x_min = min(T.LON);
x_max = max(T.LON);
y_min = min(T.LAT);
y_max = max(T.LAT);
X = linspace(x_min,x_max);
Y = linspace(y_min,y_max);
[X,Y] = meshgrid(X,Y);
% calculate contour Z
Z = SI(X,Y);
% plot the contour
contour(X,Y,Z)
% make a colorbar
cb = colorbar();
% make colorbar tick labels dates instead of days since t_min
cb.TickLabels = string(t_min+days(cb.Ticks));
% create text labels for the data points
t = text(T.LON,T.LAT,T.('ID.STN'),'HorizontalAlignment','left','VerticalAlignment','bottom');
% change the alignment of those texts that are off the plot
ext = get(t,{'Extent'});
ext = vertcat(ext{:});
set(t(ext(:,1)+ext(:,3) > x_max),'HorizontalAlignment','right')
set(t(ext(:,2)+ext(:,3) > y_max),'VerticalAlignment','top')
% "manually" make AddisAbabaBole top-aligned
t(4).VerticalAlignment = 'top';
% set axes x and y labels
c = char(176);
xlabel(['LON (' c 'E)'])
ylabel(['LAT (' c 'N)'])
% set colorbar label
ylabel(cb,'START.DATE')
I imagine the actual file has many more locations, in which case you'll probably not want to create text labels for all of them.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!