MATLAB Answers

0

Changing x-axis following use of fit and plot

Asked by OldGuyInTheClub on 18 Oct 2019 at 17:55
Latest activity Commented on by OldGuyInTheClub on 24 Oct 2019 at 20:27
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 Comments

dpb
on 21 Oct 2019 at 18:27
"Do you mean to do the appropriate conversion of datenum(datestamp) into seconds and pass that to fit?"
Sorta', but not quite...you have to pick an (can be arbitrary) reference date for datenum as well as the input seconds and then fit using it if you want to continue to use fit for its extra features of automagically returning the additional info such as confidence limits.
Since you're actually only interested in a duration from that initial time, in actuality you could then pass the difference after subtracting the initial time leaving the seconds portion only, just has have done with seconds excepting with a scaling factor.
With datenum, the integer portion is date while the fractional is time (expressed as fraction of 24-hr day). Then when you plot, you'll get either a very big number on x axis initially if you use full value for fitting or a fractional day range if use the difference. Either way, you can set the format to whatever form you choose with datetick.
For the second, make the two axes overlay
hAx(2)=axes('Position', hAx(1), 'Color','none');
hAx(1).XTick=[]; % don't have ticks on first
plot(hAx(2),datestamp,data)
should work.....it's what plotyy does, in essence.
The first approach doesn't seem like the right path given my future needs.
I wish I could make the second approach work. I adapted your code to my variables/objects. As soon as I executed the plot(hAx(2),datestamp,data) equivalent, the original plot with all the curvefitting disappears just as it did before (prior response). The hold state of the figure doesn't make a difference.
I've inserted a Live Script in two sections which I hope can show what I am doing. I am not able to paste the outputs but I am hopeful that running Section 1 and then Section 2 will show that the curve fits disappear when I try to overlay the axes.
%% Section 1: Create data, fit, and plot
clear
close all
% Create timestamp array and random data array
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');
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
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos, 'Color','none');
ax1.XTick = [];
plot(ax2,datestamp,data,'o')

Sign in to comment.

Products


Release

R2018b

1 Answer

Answer by Richard Marveldoss on 24 Oct 2019 at 19:18
 Accepted Answer

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

  1 Comment

Thanks, that worked very well. I especially appreciate the custom datatip.

Sign in to comment.