Changing x-axis following use of fit and plot

28 vues (au cours des 30 derniers jours)
OldGuyInTheClub
OldGuyInTheClub le 18 Oct 2019
Commenté : OldGuyInTheClub le 24 Oct 2019
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
OldGuyInTheClub
OldGuyInTheClub le 21 Oct 2019
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

Richard Marveldoss
Richard Marveldoss le 24 Oct 2019
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
OldGuyInTheClub
OldGuyInTheClub le 24 Oct 2019
Thanks, that worked very well. I especially appreciate the custom datatip.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Descriptive Statistics dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by