How to make a line plot and indicating data gap?
조회 수: 12 (최근 30일)
이전 댓글 표시
Dear,
I have the following datafile (please see the attachement), the data consist of three columnes, Date (YYYY-MM-DD), Time (HH:MM) and the last column is the value which need to be ploted against time. But there is gap in the data for example there is no data between 2020-10-10 to 2020-11-02 and i dont want to connect the line, but instead, i would rather leave a shaded gap and mark it as no data. Do you have any suggestions?
I have tried this code but i had problem combining the data and time; am i on the right path?
data = readtable('data.csv');
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
% Convert date and time components to serial date numbers and here i get
% error
dateNumbers = datenum(dateColumn{:}, 'yyyy-mm-dd');
timeNumbers = datenum(timeColumn{:}, 'HH:MM');
dateTime = datetime(dateNumbers + timeNumbers, 'ConvertFrom', 'datenum');
plot(dateTime, valueColumn{:});
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
Example customization options:
grid on;
datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend('Value');
댓글 수: 0
채택된 답변
Sulaymon Eshkabilov
2023년 5월 22일
편집: Sulaymon Eshkabilov
2023년 5월 22일
Here is how it can be done. Note that there are only 4 individual data points are missing and thus, they can be displayed individually.
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1390854/Data.txt');
%data = readtable('Data0.txt');
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
% Convert date and time components to serial date numbers and here i get
% error
dateNumbers = datenum(dateColumn.date);
timeNumbers = datenum(timeColumn.hr_min, 'HH:MM');
dateTime = datetime(dateNumbers + timeNumbers, 'ConvertFrom', 'datenum');
% Missing data point indices are located
IND = find(ismissing(valueColumn.value));
% Missing data points are taken to be mean of the data in order to display
% in the plot figure
valueColumn.value(IND) = mean(valueColumn.value, 'omitnan');
figure
plot(dateTime(IND), valueColumn.value(IND), 'ro','MarkerFaceColor','y', 'markersize',13);
hold on
plot(dateTime, valueColumn.value, 'b-.');
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
%Example customization options:
grid on;
hold off
% datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend( 'Missing data', 'All existing data');
댓글 수: 2
Sulaymon Eshkabilov
2023년 5월 23일
Use these commands:
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1390854/Data.txt');
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
dateNumbers = datenum(dateColumn.date);
timeNumbers = datenum(timeColumn.hr_min, 'HH:MM');
dateTime = datetime(dateNumbers, 'ConvertFrom', 'datenum');
% Missing data point indices are located
IND = find(ismissing(valueColumn.value));
% Missing data points are taken to be mean of the data in order to display
% in the plot figure
valueColumn.value(IND) = mean(valueColumn.value, 'omitnan');
figure
plot(dateTime(IND), valueColumn.value(IND), 'ro','MarkerFaceColor','y', 'markersize',13);
hold on
plot(dateTime, valueColumn.value, 'b-.');
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
%Example customization options:
grid on;
hold off
% datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend( 'Missing data', 'All existing data');
추가 답변 (1개)
Seth Furman
2023년 6월 5일
datestr is discouraged
I should mention that datestr is discouraged. Prefer datetime where possible.
For example,
dt = datetime("2020-10-01","Format","uuuu-MM-dd")
dt.Year, dt.Month, dt.Day
Consider xregion in MATLAB R2023a or later
% Read data as a timetable
tt = readtimetable("Data.txt", TextType="string", VariableNamingRule="preserve")
% Convert duration strings to duration objects
tt.("hr:min") = duration(tt.("hr:min"), InputFormat="hh:mm")
% Move duration data to timetable row-times
tt.date = tt.date + tt.("hr:min");
tt.date.Format = "uuuu-MM-dd HH:mm"; % Update datetime format to include hours and minutes
tt.("hr:min") = [] % Remove "hr:min" variable, which is no longer needed
% Plot timetable data and add an xregion with legend entry labeling it as
% having no data
plot(tt, "value");
xregion(datetime(2020,10,10), datetime(2020,11,02), DisplayName="No data");
xtickformat("uuuu-MM-dd HH:mm");
xtickangle(45);
legend(["value" "No data"]);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!