Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}]]
        }
    }
]
share|improve this question
    
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. –  Szabolcs Mar 14 '12 at 15:50
    
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. –  VLC Mar 14 '12 at 16:12
    
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. –  Szabolcs Mar 14 '12 at 16:31
    
Yes, I agree, your suggestion certainly solves the issue. I was however convinced that the first function in Show always controls the graphics settings. –  VLC Mar 14 '12 at 16:36
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
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. –  VLC Mar 14 '12 at 16:28
add comment

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

share|improve this answer
    
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. –  VLC Mar 14 '12 at 16:22
    
{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. –  Brett Champion Mar 14 '12 at 16:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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