How can I plot this X and Y data?

6 views (last 30 days)
Hi coders!
I have a very simple data (Data is attached as a .mat file) set but I want to know the most efficient way of plotting it.
I have a few tide height (m) data and I want to plot this in an hour-based manner. In X axis, I want to keep hours (00:00, 01:00, .... 23:00, 24:00) and in Y-axis, I want to keep the heights. The data is in a table format and given below -
'00:00' 0.820000000000000
'01:00' 1.11600000000000
'01:48' NaN
'02:00' 1.23800000000000
'03:00' 1.11400000000000
'04:00' 0.799000000000000
'05:00' 0.428000000000000
'06:00' 0.106000000000000
'07:00' -0.0750000000000000
'07:18' NaN
'08:00' -0.0330000000000000
'09:00' 0.178000000000000
'10:00' 0.413000000000000
'11:00' 0.646000000000000
'12:00' 0.936000000000000
'13:00' 1.23000000000000
'14:00' 1.39400000000000
'14:30' NaN
'15:00' 1.33300000000000
'16:00' 1.06400000000000
'17:00' 0.704000000000000
'18:00' 0.363000000000000
'19:00' 0.101000000000000
'19:54' NaN
'20:00' -0.00200000000000000
'21:00' 0.0770000000000000
'22:00' 0.222000000000000
'23:00' 0.369000000000000
'00:00' 0.589000000000000
'01:00' 0.898000000000000
'02:00' 1.16400000000000
'03:00' 1.23200000000000
'04:00' 1.04800000000000
'05:00' 0.698000000000000
'06:00' 0.330000000000000
'07:00' 0.0420000000000000
'07:48' NaN
'08:00' -0.0820000000000000
'09:00' 0.0220000000000000
'10:00' 0.262000000000000
'11:00' 0.491000000000000
'12:00' 0.711000000000000
'13:00' 0.990000000000000
'14:00' 1.26400000000000
'15:00' 1.38800000000000
'15:12' NaN
'16:00' 1.27300000000000
'17:00' 0.959000000000000
'18:00' 0.586000000000000
'19:00' 0.259000000000000
'20:00' 0.0410000000000000
'20:30' NaN
'21:00' 0.00500000000000000
'22:00' 0.136000000000000
'23:00' 0.292000000000000
Any feedback from you will be greatly appreciated!! <3

Accepted Answer

Star Strider
Star Strider on 27 Jan 2023
Perhaps something like this —
LD = load(websave('Tide_Data','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1276250/Tide_Data.mat'));
Tide_info = LD.Tide_info
Tide_info = 10048×3 table
Date Time_GMT_ Predicted_m_ ______________ _________ ____________ {'2021/01/01'} {'00:00'} 0.82 {'2021/01/01'} {'01:00'} 1.116 {'2021/01/01'} {'01:48'} NaN {'2021/01/01'} {'02:00'} 1.238 {'2021/01/01'} {'03:00'} 1.114 {'2021/01/01'} {'04:00'} 0.799 {'2021/01/01'} {'05:00'} 0.428 {'2021/01/01'} {'06:00'} 0.106 {'2021/01/01'} {'07:00'} -0.075 {'2021/01/01'} {'07:18'} NaN {'2021/01/01'} {'08:00'} -0.033 {'2021/01/01'} {'09:00'} 0.178 {'2021/01/01'} {'10:00'} 0.413 {'2021/01/01'} {'11:00'} 0.646 {'2021/01/01'} {'12:00'} 0.936 {'2021/01/01'} {'13:00'} 1.23
Tide_info.Date = datetime(Tide_info.Date, 'InputFormat','yyyy/MM/dd');
Tide_info.Time_GMT_ = datetime(Tide_info.Time_GMT_, 'InputFormat','HH:mm');
DateTime = Tide_info.Date + timeofday(Tide_info.Time_GMT_); % Combine Date & Time Variables
Tide_info = addvars(Tide_info, DateTime, 'After','Time_GMT_') % Add As A New Variable
Tide_info = 10048×4 table
Date Time_GMT_ DateTime Predicted_m_ ___________ ____________________ ____________________ ____________ 01-Jan-2021 27-Jan-2023 00:00:00 01-Jan-2021 00:00:00 0.82 01-Jan-2021 27-Jan-2023 01:00:00 01-Jan-2021 01:00:00 1.116 01-Jan-2021 27-Jan-2023 01:48:00 01-Jan-2021 01:48:00 NaN 01-Jan-2021 27-Jan-2023 02:00:00 01-Jan-2021 02:00:00 1.238 01-Jan-2021 27-Jan-2023 03:00:00 01-Jan-2021 03:00:00 1.114 01-Jan-2021 27-Jan-2023 04:00:00 01-Jan-2021 04:00:00 0.799 01-Jan-2021 27-Jan-2023 05:00:00 01-Jan-2021 05:00:00 0.428 01-Jan-2021 27-Jan-2023 06:00:00 01-Jan-2021 06:00:00 0.106 01-Jan-2021 27-Jan-2023 07:00:00 01-Jan-2021 07:00:00 -0.075 01-Jan-2021 27-Jan-2023 07:18:00 01-Jan-2021 07:18:00 NaN 01-Jan-2021 27-Jan-2023 08:00:00 01-Jan-2021 08:00:00 -0.033 01-Jan-2021 27-Jan-2023 09:00:00 01-Jan-2021 09:00:00 0.178 01-Jan-2021 27-Jan-2023 10:00:00 01-Jan-2021 10:00:00 0.413 01-Jan-2021 27-Jan-2023 11:00:00 01-Jan-2021 11:00:00 0.646 01-Jan-2021 27-Jan-2023 12:00:00 01-Jan-2021 12:00:00 0.936 01-Jan-2021 27-Jan-2023 13:00:00 01-Jan-2021 13:00:00 1.23
VN = Tide_info.Properties.VariableNames;
figure
plot(Tide_info.DateTime, Tide_info.Predicted_m_)
grid
xlabel(VN{3})
ylabel(strrep(VN{4},'_','\_'))
title(strrep('Tide_info','_','\_'))
.
  2 Comments
Ashfaq Ahmed
Ashfaq Ahmed on 27 Jan 2023
Hi! This is really great! But I wanted to keep the hours (00:00 to 24:00) in the x-axis and all the co-responding height for those hours in the Y axis. Can you please give me an idea on what part of your code do I need to change to get the plot?
Star Strider
Star Strider on 27 Jan 2023
Thank you!
That is certainly possible, however even using retime on a timetable version of your data to take the hourly mean creates an extremely large (8760 rows) result.
LD = load(websave('Tide_Data','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1276250/Tide_Data.mat'));
Tide_info = LD.Tide_info;
Tide_info.Date = datetime(Tide_info.Date, 'InputFormat','yyyy/MM/dd');
Tide_info.Time_GMT_ = datetime(Tide_info.Time_GMT_, 'InputFormat','HH:mm');
DateTime = Tide_info.Date + timeofday(Tide_info.Time_GMT_); % Combine Date & Time Variables
Tide_info = addvars(Tide_info, DateTime, 'After','Time_GMT_'); % Add As A New Variable
VN = Tide_info.Properties.VariableNames;
TTide_info = removevars(Tide_info, [1 2]);
TTide_info = table2timetable(TTide_info);
TTide_info = retime(TTide_info, 'hourly', @(x)mean(x,'omitnan'))
TTide_info = 8760×1 timetable
DateTime Predicted_m_ ____________________ ____________ 01-Jan-2021 00:00:00 0.82 01-Jan-2021 01:00:00 1.116 01-Jan-2021 02:00:00 1.238 01-Jan-2021 03:00:00 1.114 01-Jan-2021 04:00:00 0.799 01-Jan-2021 05:00:00 0.428 01-Jan-2021 06:00:00 0.106 01-Jan-2021 07:00:00 -0.075 01-Jan-2021 08:00:00 -0.033 01-Jan-2021 09:00:00 0.178 01-Jan-2021 10:00:00 0.413 01-Jan-2021 11:00:00 0.646 01-Jan-2021 12:00:00 0.936 01-Jan-2021 13:00:00 1.23 01-Jan-2021 14:00:00 1.394 01-Jan-2021 15:00:00 1.333
figure
plot(TTide_info.DateTime, TTide_info.Predicted_m_)
grid
xlabel(VN{3})
ylabel(strrep(VN{4},'_','\_'))
title(strrep('TTide_info','_','\_'))
figure
plot(TTide_info.DateTime, TTide_info.Predicted_m_)
grid
xlabel(VN{3})
ylabel(strrep(VN{4},'_','\_'))
title(strrep('TTide_info','_','\_'))
Ax = gca;
Ax.XTick = TTide_info.DateTime(1:25,1);
xlim([TTide_info.DateTime(1,1), TTide_info.DateTime(25,1)])
It would not be possible to plot all of them in the first plot (as I plotted in the expanded second plot) and have them all visible and readable. The information is available in the ‘TTide_info’ timetable and can be plotted with either an extremely long plot, or as individual plots, such as I did here.
You can change the first plot to be the same as the second plot, however the result is likely not what you want. (Actually, I cannot even force that here, so I posted the appropriate code, however commented-out. Please experiment with it offline, since I cannot display it here.)
% figure
% plot(TTide_info.DateTime, TTide_info.Predicted_m_)
% grid
% xlabel(VN{3})
% ylabel(strrep(VN{4},'_','\_'))
% title(strrep('TTide_info','_','\_'))
% Fg = gcf;
% pos = Fg.Position;
% Fg.Position = pos + [-500 0 900 0];
% HourInterval = 6;
% Ax.XTick = TTide_info.DateTime(1:HourInterval:end,1); % Change 'HourInterval' To Plot The Desired REsult
.

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 27 Jan 2023
It's unclear to me exactly what you want to plot. That file has 10,048 data points.
Here is one possibility, which puts a dot at each time point, with the time (but not the date) on the x-axis. Or, did you want to only plot a segment of the data, but not have data from different dates in the same column?
load("Tide_Data.mat")
plot(datetime(Tide_info.Time_GMT_,"InputFormat","HH:mm"),Tide_info.Predicted_m_,'.')
  1 Comment
Ashfaq Ahmed
Ashfaq Ahmed on 27 Jan 2023
Hi! Excellent! That's exactly what I wanted to plot.
Actually, the tide height data were taken in every one hour for the whole year 2021. That's why there are so many (10,048) data point. I wnated to plot the height with respect to hours of the day.

Sign in to comment.

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!