6
$\begingroup$

Suppose I have these two sets of data:

data1 = {{0, 0}, {5, 0}, {10, .1}, {15, .3}, {20, .7}, {25, 1}, {30, 1}};
data2 = {{0, 0}, {5, 0}, {10, 0}, {15, 0}, {20, 0}, {25, .1}, {30, .3}};

and I fit them with a cumulative Gaussian function:

cumulativeGauss[x_, μ_, Σ_] := 
    CDF[NormalDistribution[μ, Σ], x]

I obtain the parameters that I'll use to plot these functions:

fit1 = FindFit[
    data1, cumulativeGauss[x, μ, Σ],
    {{μ, 5}, {Σ, 5}}, x][[All, 2]];
fit2 = FindFit[
    data2, cumulativeGauss[x, μ, Σ],
    {{μ, 5}, {Σ, 5}}, x][[All, 2]];

Here below are all the plots that I've produced to show the problem. The left column shows that when only the Plot command is used the function is plotted along the whole range, so no problem. On the other hand, the right column shows the figures produced by combining a ListPlot with a Plot. The first figure shows no problem, however, the figure in the second row does not plot the function along the whole range. This problem can be solved by increasing the range over which the function is plotted (figure in the 3rd row). Any hint about why this problem occurs and/or if there is any other solution?

enter image description here

Here the the code to generate the figures:

GraphicsGrid[
    {
        {
        Plot[cumulativeGauss[x, fit1[[1]], fit1[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data1, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit1[[1]], fit1[[2]]], {x, 0, 30}]]
        },

        {
        Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data2, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30}]]
        },
        {
        Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data2, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 40}]]
        }
    }
]
$\endgroup$
  • $\begingroup$ In the second row you didn't set the same plot range in Plot in the Show as before, this is what causes the cutoff. Don't use PlotRange in ListPlot, but do use it in Plot. $\endgroup$ – Szabolcs Mar 14 '12 at 15:50
  • $\begingroup$ I'm not sure that I understand your comment on the plot range in the second row. The first two rows have the same plot ranges, but only in the first row the function is plotted correctly. $\endgroup$ – VLC Mar 14 '12 at 16:12
  • $\begingroup$ I mean, do include the PlotRange specification in Plot as well, not only in ListPlot. Actually their order in Show doesn't matter. I'm sorry if I'm not clear, I'm a bit sleep deprived. $\endgroup$ – Szabolcs Mar 14 '12 at 16:31
  • $\begingroup$ Yes, I agree, your suggestion certainly solves the issue. I was however convinced that the first function in Show always controls the graphics settings. $\endgroup$ – VLC Mar 14 '12 at 16:36
8
$\begingroup$

I'm assuming you want to use the same PlotRange for cell (2,2) as for the other cells.

You can change the order: show Plot before ListPlot. Note that Plot will now set the PlotRange; as I understand it, the first function called by Show controls the graphics settings (if there should be a conflict).

Show[Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},  
           PlotRange -> {{0, 30}, {0, 1}}],
     ListPlot[data2]
    ]

The following questions remain:

  • "Why isn't the full plot range displayed when ListPlot comes before Plot?
  • "Why can't you specify the PlotRange in Show as follows?"

    Show[Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30}], ListPlot[data2], PlotRange -> {{0, 30}, {0, 1}}] According to the documentation, this latter approach should work (but it does not):

instruction

Either of these result in the following:

PlotRange2


Edit

You can display the data points beneath the model using. Note, however that the points will lie beneath the axes.

Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},  
     PlotRange -> {{0, 30}, {0, 1}},
     PlotRangePadding -> {{.3, 3}, {0.1, 0}},
     Prolog -> {Red, PointSize[Large], Point[data2]}]

points shown

$\endgroup$
  • $\begingroup$ I was aware that changing the order of Plot and ListPlotsolves the problem, but I bumped in this problem because I prefer to plot the fitted function over the data and not the other way around. Why the options specified in Show do not override those included in the graphics expression is thus still a mystery. $\endgroup$ – VLC Mar 14 '12 at 16:28
5
$\begingroup$

The issue is that

Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30}]

enter image description here

is clipping where it goes off the top of the vertical plot range. Forcing that plot to PlotRange -> {{0, 30}, {0, 1}} like the others will solve the problem:

GraphicsGrid[
    {
        {
        Plot[cumulativeGauss[x, fit1[[1]], fit1[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data1, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit1[[1]], fit1[[2]]], {x, 0, 30}]]
        },

        {
        Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data2, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30}, 
                 PlotRange -> {{0, 30}, {0, 1}}]]
        },
        {
        Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 30},
            PlotRange -> {{0, 30}, {0, 1}}],
        Show[ListPlot[data2, PlotRange -> {{0, 30}, {0, 1}}],
            Plot[cumulativeGauss[x, fit2[[1]], fit2[[2]]], {x, 0, 40}]]
        }
    }
]

enter image description here

$\endgroup$
  • $\begingroup$ Now I know at least why the cutoff occurs at that specific location. However, being forced to use multiple definitions of the plot range might be slightly unhandy in certain instances. $\endgroup$ – VLC Mar 14 '12 at 16:22
  • $\begingroup$ {p = Plot[...], Show[ListPlot[...], p]} should work. Or creating a function that takes data and fit and constructs each row. With the latter, it's then very easy to set options for all the plots. $\endgroup$ – Brett Champion Mar 14 '12 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.