How can I plot this X and Y data?
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2023-1-27
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.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;
figure
plot(Tide_info.DateTime, Tide_info.Predicted_m_)
grid
xlabel(VN{3})
ylabel(strrep(VN{4},'_','\_'))
title(strrep('Tide_info','_','\_'))
.
2 个评论
Star Strider
2023-1-27
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'))
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
.
更多回答(1 个)
the cyclist
2023-1-27
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_,'.')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!