Changing x-axis following use of fit and plot
17 次查看(过去 30 天)
显示 更早的评论
I am using fit as described in https://www.mathworks.com/help/curvefit/fit.html to fit and plot a timestamp (in seconds from a base epoch) vs. data along with the fit and confidence values. I can't copy/paste results unforunately so this will have to be descriptive.
% timestamp = 1d array of doubles: seconds elapsed since 1/1/1970; ~1.57E9 sec
% data = 1d array of doubles: one data point per timestamp point; ~1E-10
curvefit = fit(timestamp, data, 'poly1', 'normalize','on'); % Generate fit
plot(curvefit, timestamp, data, 'o', 'predfunc') ; % Generate plot showing data, fit, and confidence bounds
For the plot, I'd like the x-axis to be dates. I can convert the timestamp to a datestamp using
datestamp = datetime(timestamp, 'ConvertFrom', 'epochtime', 'Epoch', epoch) ; % with epoch defined elsewhere
I tried getting the axis limits for the gcf and updating:
lim = axis;
axis( [datestamp(1) datestamp(end) lim(3) lim(4)] ); % Change x-axis, keep y-axis
The error message says all inputs must be datetimes, date/time character vectors, or date/time strings.
xlim([datestamp(1) datestamp(end)])
The error message says that xlim requires numeric inputs.
Interestingly, I can generate a data vs. datestamp plot:
plot(datestamp, data,'o')
but this does not have the benefit of autogenerated fits, confidence bounds, and legends.
I'm missing something simple...
7 个评论
采纳的回答
Richard Marveldoss
2019-10-24
Hello there,
At this moment it is not directly possible to use "datetime" along with the "curvefit" function. The issue with using "datetime" for x axis is tricky because the curve fit and the confidence bound lines are all based of the "timestamp" (a double variable from the code added by OldGuyInTheClub on 21 Oct 2019) through which the lines were generated in the first place and changing it "datetime" is not compatible .
A workaround for this issue is to change the "XTickLabels" which changes the values displayed but the plots still use the "timestamp" values. The XTick values of the plot obtained using the "fit" function is obtained and transformed to "datetime" string format and assigned to the "XTickLabels". Additionally custom code has been added to render data-tip as desired since the default one returns the X axis value in the double format and need to transformed to "datetime" type.
Please refer to the modified code below that incorporates the workaround mentioned.
%% Section 1: Create data, fit, and plot
clear
close all
% Create timestamp array and random data array
f1=figure;
timestamp = (1.5710E9:0.001E9:1.6900E9)' ;
data = rand(120,1)*1E-9 ;
plot(timestamp, data,'o')
xlabel('Time data')
% Define epoch as 1/1/1970 and create a datestamp...
% array from timestamp
epoch = '1970-01-01';
datestamp = datetime(timestamp, 'ConvertFrom', 'epochtime', 'Epoch', epoch) ;
plot(datestamp, data,'o')
xlabel('Date stamp')
% Generate a fit curve using the timestamp as the x-axis
% and plot the data, fit, and confidence limits
curvefit = fit(timestamp, data, 'poly1', 'normalize','on');
p=plot(curvefit, timestamp, data, 'o', 'predfunc') ;
xlabel('Time stamp')
title('Results from curve fit')
%% Section 2
% Now let's try to change the x-axis to display the
% datetime
f2=figure;
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos, 'Color','none');
ax1.XTick = [];
plot(ax2,datestamp,data,'o')
%% workaround
test=f1.Children(2).XAxis.TickValues;
dummy=datetime(test, 'ConvertFrom', 'epochtime', 'Epoch', epoch);
dummy=datestr(dummy,'mmm yyyy');
f1.Children(2).XAxis.TickLabels=dummy
dcm_obj = datacursormode(f1);
set(dcm_obj,'UpdateFcn',@myupdatefcn)
%custom datatip function
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
epoch = '1970-01-01';
pos = get(event_obj,'Position');
datestamp = datetime(pos(1), 'ConvertFrom', 'epochtime', 'Epoch', epoch) ;
txt = {['X: ',datestr(datestamp,'mmm yyyy')],...
['Y: ',num2str(pos(2))]};
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!