MATLAB Answers

Plotting two data sets using the Curve Fitting Tool

16 views (last 30 days)
Elliza Feisol
Elliza Feisol on 22 Aug 2020
Edited: dpb on 25 Aug 2020
I was wondering is it possible two fit two different data sets in the same plot? The data sets are scatter plots and I have fitted a 4th polynomial through both sets seen attached. I would like to combine the two plots into one. Is there anyway to do so? I tried using the hold function but it did not work.


dpb on 22 Aug 2020
No can do. It lets you compare fits of different types for a given data set to help in model selection for the one set, but doesn't have the facility to do multiple datasets at the same time.
Just use the command line tools...if you wanted, you could save the desired fits from the tool (remember to save with names so can recognize which is which) and then just evaluate the fits for the final plot.
I'd look carefully at the statistics for the coefficients, though -- a 4th order to those data would make me suspicious are overfitting.
Elliza Feisol
Elliza Feisol on 22 Aug 2020
Alright, thank you for the recommemdation. I've plotted the data using another code, however I'm having trouble fitting a line through the points. Is there anyway to include the polynomial fit for the two sets of data?
healthydata = scatter(PIVlabLocation1.VarName38,PIVlabLocation1.VarName39);
hold on
unhealthydata = scatter(G0801location1.AVERAGE,G0801location1.VarName21);
hold off
xlabel ('Y*')
ylabel ('U*')
title('Comparison of healthy and unhealthy data')

Sign in to comment.

Accepted Answer

dpb on 22 Aug 2020
Edited: dpb on 24 Aug 2020
Well, the old-fashioned way if all you wanted was the coefficients would be to use polyfit and polyval ...but since you have the TB, why not just use fit and get the statistics and builtin behavior automagically?
hold on
set(hLH,{'color'},{'b'}) % healthy blue
set(hLUH,{'color'},{'r'}) % unhealthy red
vals=[{'o';'o'} get([hLH(1) hLUH(1)],{'color'})]; % marker circle, same color as line face color
set([hLH(1) hlUH(1)],{'Marker','MarkerFaceColor'},vals)
title('Comparison of healthy and unhealthy data')


Show 5 older comments
dpb on 24 Aug 2020
There was still a hL1, hL2 in the first that missed when renamed on the fly to hLH, hLUH in the first answer...hadn't noticed until now.
Elliza Feisol
Elliza Feisol on 24 Aug 2020
Sorry to bother again, but is there anyway to extend the polyline beyond the data points? I've attached the current figure as well as how I want the graph to look.
dpb on 25 Aug 2020
Sure -- two ways to approach; both entail adding points to be evaluated over the extended range you want the plot drawn over.
  1. The fit object plot() routine:
newX=[newpoints1 X newpoints2];
hL1=plot(fitObject,newX,[nan(size(newpoints1)) data nan(size(newpoints1))]);
where newpoints1/2 are the locations outside the current range of independent variable you want to add...the X and the observed data vectors have to be same size here so must augment the data vector with NaN to match. As we've noted before, the NaN won't show on the plot.
2. The "traditional" plot() function:
hold on % altho should be already, to remind just in case
newY1=fitObject(newpoints1); % evaluate the fitted curve over
newY2=fitObject(newpoints2]; % the two new ranges wanted to add
hL3=plot(newpoints1,newY1); % add the two new ranges to the existing graph
hL4=plot(newpoints2,newY2); % first point in ranges should be existing line ends
Set linestyle, etc., as desired to match.
What isn't at all clear to me is how you get the second plot -- the range for the first covers the desired x-axis values, but the data points are scaled differently in the second and don't cover that range. Are those completely independent fits of a scaled variable in the second vis a vis the first, I guess?
Also, the first makes sense that the wider bell shape has lower peak and higher tails whereas the second shows the wider (blue) having both wider tails and being (slightly) taller at the peak. That wouldn't seem to normalize correctly to conserve area???
But, I have no idea what any of this is/means, but just looks strange to this dumb ol' nuclear engineer is all :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by