# ListPlot within Plot   (example post)

**URL:** <https://community.wolfram.com/t/listplot-within-plot-example-post/81>\
**Category:** Questions\
**Tags:** data-science, mathematica, graphics-and-visualization\
**Created:** [October 15, 2012, 6:30pm UTC](https://community.wolfram.com/t/listplot-within-plot-example-post/81 "2012-10-15T18:30:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [October 15, 2012, 6:30pm UTC](https://community.wolfram.com/t/listplot-within-plot-example-post/81/1 "2012-10-15T18:30:19Z")

</div>

I want to place a 2D plot of fit residuals in a corner of a plot of the data+fitted curve.

---

<div class="post-metadata">

**Author:** ![brucem](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brucem/32/26319_2.png) [@brucem](https://community.wolfram.com/u/brucem)\
**Post date:** [October 15, 2012, 7:16pm UTC](https://community.wolfram.com/t/listplot-within-plot-example-post/81/2 "2012-10-15T19:16:00Z")

</div>

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 Initialization

```auto
In[1]:= 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

```auto
In[2]:= linfitmodel = b*x + c; 

In[3]:= linfitparams = FindFit[dat, linfitmodel, {b, c}, x]
Out[3]= {b->0.0916195,c->1.69788}

In[4]:= linfitresids = dat - Table[{0, linfitmodel /. linfitparams}, {x, 0, 10}]; 

In[5]:= 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

```auto
In[6]:= quadfitmodel = a*x^2 + b*x + c; 

In[7]:= quadfitparams = FindFit[dat, quadfitmodel, {a, b, c}, x]
Out[7]= {a->-0.0309372,b->0.400991,c->1.23382} 

In[8]:= quadfitresids = dat - Table[{0, quadfitmodel /. quadfitparams}, {x, 0, 10}]; 
In[9]:= 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.

```auto
In[1]:= ?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.

```
