Message Boards Message Boards

0
|
4391 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Combine a Fit[] plot with DateListPlot?

Posted 8 years ago

I've got a SQL query that returns the list datedWeights, being dates and weights in the form...

{{"2016-06-02", 84.4}, {"2016-06-03", 83.7},....

I've used Fit[] to get a fit curve for the weights, and DateListPlot to plot weight by date....

weights = #[[2]] & /@ datedWeights;
fit = Fit[weights, {1, x, x^2}, x ];
fitPlot = Plot[fit, {x, 0, Length[datedWeights]}];
dateListPlot = DateListPlot[datedWeights];
Show[{fitPlot, dateListPlot}]
Show[{dateListPlot, fitPlot}]

But both calls to Show[] only show one plot or the other. What I would like is the fit data plotted over the weights, with the x-axis showing dates.

Any suggestions?

POSTED BY: Brad Varey
2 Replies

I think the problem is the horizontal scales of the two plots, which are completely different. I would suggest working with absolute time in both the fit and the plot of the fitting parabola:

Clear[datedWeights, fit, weights, fitPlot];
datedWeights = {{"2016-06-02", 84.4}, {"2016-06-03", 
    83.7}, {"2016-06-04", 82.7}};
weights = {AbsoluteTime[#[[1]]], #[[2]]} & /@ datedWeights;
fit[x_] = Fit[weights, {1, x, x^2}, x];
fitPlot = 
  DateListPlot[
   Table[{x, fit[x]}, {x, AbsoluteTime[datedWeights[[1, 1]]], 
     AbsoluteTime[datedWeights[[-1, 1]]], 3600}], PlotStyle -> Red];
dateListPlot = DateListPlot[datedWeights, PlotRange -> All];
Show[{fitPlot, dateListPlot}]
Show[{dateListPlot, fitPlot}]
POSTED BY: Gianluca Gorni
Posted 8 years ago

Thanks, Gianluca, that does seem to do the trick!

POSTED BY: Brad Varey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract