Changing x-axis following use of fit and plot

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 commentaires

dpb
dpb le 19 Oct 2019
What's a timestamp? fit(x,y,...) x is according to above link
Data to fit, specified as a matrix with either one (curve fitting) or two (surface fitting) columns. You can specify variables in a MATLAB table using tablename.varname. ...
...
Data Types: double
timestamp is a 1d array of doubles: seconds elapsed since 1/1/1970; The numbers are in the range of 1.57E9 sec
data is a 1d array of doubles: one data point per timestamp point; The numbers are in the range of 1E-10
The fit function returns the object curvefit as expected and I can plot the data vs. timestamp, the best fit, and confidence bounds easily with the commands shown.
The only problem is how to change the x-axis from elapsed time numbers to datestamp values.
[Original post updated with clarifications]
dpb
dpb le 19 Oct 2019
Modifié(e) : dpb le 19 Oct 2019
OK, you fitted and plotted against a double value...you can convert the time into datetimes or durations; you must then plot using that as the x argument in plot() for it to recognize the x-axis is to be a datetimeruler object. Once you do that, THEN you can set limits as you wish, but you can't set limits on a double axis in terms of datetime variables as you've tried to do nor can you set the limits on a plot with datetimes as the axis values with doubles--they must be self-consistent.
plot() has versions that recognize the fit() returned object--problem is in your case that there isn't a version of fit() that recognizes datetime; only doubles. Two ways to proceed:
  1. Use the venerable datenum instead of datetime for the fit function time input. It is just a double underneath. When plot() with it, then must use datetick to set the display of the axis to time units. Klunky, but can be done.
  2. Use a second axis overlaying the first and plot the data against the datetime result on it, turning the axes of the first off so see the time values displayed, not the underlying seconds as double.
OldGuyInTheClub
OldGuyInTheClub le 21 Oct 2019
Modifié(e) : OldGuyInTheClub le 21 Oct 2019
Thanks for your suggestions:
1) Not sure how to implement this. My time vector is already a 1d array of doubles of elapsed seconds since 1/1/1970. If I try using datenum on the datestamp variable (ie. timestamp converted to datestamp), it returns days since 1/1/0000. Do you mean to do the appropriate conversion of datenum(datestamp) into seconds and pass that to fit?
recognizing that page is for line objects and not plot objects. With a plot up (timestamp x, data y, with fit and confidence limits)
hold on ; % Hopefully preserve data, fits, and confidence bounds
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos, 'XAxisLocation','bottom','YAxisLocation',...
'right','Color','none'); % New bottom x-axis overlaid on existing one
plot(ax2, datestamp, data, 'o'); % Replot but with datestamps on x-axis
ax1.Visible = off; % Turn off original timestamp x-axis
I get a plot of data vs. datestamp but the data, fit, and confidence bounds disappear.
"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.
OldGuyInTheClub
OldGuyInTheClub le 21 Oct 2019
Modifié(e) : OldGuyInTheClub le 21 Oct 2019
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')

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaire

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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by