2
|
37220 Views
|
|
4 Total Likes
View groups...
Share
GROUPS:

# ListPlot within Plot (example post)

Posted 12 years ago
 I want to place a 2D plot of fit residuals in a corner of a plot of the data+fitted curve.
 In Mathematica 6 and later there is a special function, Inset, for that purpose. Here is a simple, realistic example. The residuals of a fit are the differences between the data and the fitted curve. Data InitializationIn:= dat = {{0, 1.28086}, {1, 1.56017}, {2, 1.91442}, {3, 2.09588}, {4, 2.41924},                {5, 2.37783}, {6, 2.57325}, {7, 2.58195}, {8, 2.43806}, {9, 2.34434},                {10, 2.12974}}; Fit to a Line  In:= linfitmodel = b*x + c;   In:= linfitparams = FindFit[dat, linfitmodel, {b, c}, x] Out= {b->0.0916195,c->1.69788}  In:= linfitresids = dat - Table[{0, linfitmodel /. linfitparams}, {x, 0, 10}];   In:= Show[ListPlot[dat, PlotStyle -> Directive[Red, PointSize]],               Plot[Evaluate[linfitmodel /. linfitparams], {x, 0, 10}],              Graphics[Inset[                  ListLinePlot[linfitresids, ImageSize -> 100, PlotLabel -> "Residuals"],                 {8, 1.5}]],              AxesOrigin -> {0, 1}, PlotRange -> All] The residuals curve shows structure - down at the ends and high in the middle. This suggests that a higher-order term might be useful. Fit to a Quadratic Curve In:= quadfitmodel = a*x^2 + b*x + c;   In:= quadfitparams = FindFit[dat, quadfitmodel, {a, b, c}, x] Out= {a->-0.0309372,b->0.400991,c->1.23382}   In:= quadfitresids = dat - Table[{0, quadfitmodel /. quadfitparams}, {x, 0, 10}];  In:= Show[ListPlot[dat, PlotStyle -> Directive[Red, PointSize]],               Plot[Evaluate[quadfitmodel /. quadfitparams], {x, 0, 10}],               Graphics[Inset[                ListLinePlot[quadfitresids, ImageSize -> 100, PlotLabel -> "Residuals"],                 {8, 1.5}]],             AxesOrigin -> {0, 1}, PlotRange -> All] The residuals are small and random, which suggests a decent fit.   In:= ?Inset        Inset[obj] represents an object obj inset in a graphic.      Inset[obj, pos] specifies that the inset should be placed at              position pos in the graphic.      Inset[obj, pos, opos] aligns the inset so that position opos              in the object lies at position pos in the enclosing graphic.      Inset[obj, pos, opos, size] specifies the size of the inset in the              coordinate system of the enclosing graphic.       Inset[obj, pos, opos, size, dirs] specifies that the axes of the            inset should be oriented in directions dirs.