6
$\begingroup$

I have this code here:

Plot[MapThread[
Function[{\[Mu], \[Sigma]}, 
 PDF[NormalDistribution[\[Mu], \[Sigma]], x]], {{1,2,3,4,5}, {0.5,1.0,1.5,2.0,2.5}}] // Evaluate, {x, -10, 10}, 
  Filling -> Axis, 
PlotLegends -> 
LineLegend[{"Five Years", "Three Years", "One Year", "Six Months", 
 "Three Months"}], 
PlotStyle -> {Thickness[.001], Thickness[.002], Thickness[.003], 
Thickness[.004], Thickness[.005]}]

which creates five bell curves in a single graph. I would like to add a vertical line at the mean of each curve (at its peak) that begins at the curve and ends at the x-axis. In other words, a vertical line at x=1, x=2 ... x=5. Could I gain any insight into how this could be accomplished?

$\endgroup$
10
$\begingroup$

Following D.G. Stork:

Plot[
MapThread[Function[{\[Mu], \[Sigma]}, PDF[NormalDistribution[\[Mu], \[Sigma]], x]], {{1, 2, 3, 4,5}, {0.5, 1.0, 1.5, 2.0, 2.5}}] // Evaluate, {x, -10, 10}, 
Filling -> Axis,PlotLegends -> LineLegend[{"Five Years", "Three Years", "One Year", "Six Months", 
"Three Months"}], 
PlotStyle -> {Thickness[.001], Thickness[.002], Thickness[.003], 
Thickness[.004], Thickness[.005]}, PlotRange -> {{0, 10}, All}, 
Epilog -> ({Red, Dashing[0.01], 
            Line@MapThread[{{#1, 0}, {#1, #2}} &, 
            {
            {1, 2, 3, 4, 5}, 
            Evaluate@(MapThread[Function[{\[Mu], \[Sigma]}, 
            PDF[NormalDistribution[\[Mu], \[Sigma]], \[Mu]]], {{1, 2,3, 4, 5}, {0.5, 1.0, 1.5, 2.0, 2.5}}])
            } 
            ]
          })
     ]

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ What does the filling between the density curve and the axis designate? $\endgroup$ – wolfies Nov 15 '17 at 16:21
  • $\begingroup$ You can remove it from the graph if you want. Nothing special, except you wanted to indicate or assign something useful to the area below the curve. $\endgroup$ – José Antonio Díaz Navas Nov 15 '17 at 18:33
8
$\begingroup$
Epilog-> Line[{{#,0},{#,.25}}]& /@ Range[5]

or more generally

Epilog-> Line[{{#,0},{#,.25}}]& /@ {1,2,3,4,5}

where the list is the list of the means.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Note that you can use HalfLine if you want a line that doesn't terminate. $\endgroup$ – Sjoerd Smit Nov 14 '17 at 9:34
3
$\begingroup$

You could also combine a Plot[] and a ListPlot[] with the setting Filling -> Axis:

Show[Plot[MapThread[
     Function[{μ, σ}, PDF[NormalDistribution[μ, σ], x]],
              {{1, 2, 3, 4, 5}, {0.5, 1.0, 1.5, 2.0, 2.5}}] // Evaluate,
          {x, -10, 10}, Filling -> Axis, 
          PlotLegends -> LineLegend[{"Five Years", "Three Years", "One Year",
                                     "Six Months", "Three Months"}], PlotRange -> All, 
          PlotStyle -> {Thickness[.001], Thickness[.002], Thickness[.003],
                        Thickness[.004], Thickness[.005]}], 
     ListPlot[MapThread[
     Function[{μ, σ}, {{μ, PDF[NormalDistribution[μ, σ], μ]}}],
              {{1, 2, 3, 4, 5}, {0.5, 1.0, 1.5, 2.0, 2.5}}], Filling -> Axis, 
          FillingStyle -> Transpose[{{Thickness[.001], Thickness[.002], Thickness[.003],
                                      Thickness[.004], Thickness[.005]}}]],
     Axes -> None, Frame -> True]

various bell curves

$\endgroup$
2
$\begingroup$

For completeness here's an approach with GridLines (that fails the peak-to-axis criterion) and another combining two plots, the latter with a Filling->Axis option. Although I don't necessarily think these are better solutions they can potentially reduce a cluttered Plot command - I would personally favour the first if I wanted a quick solution and the second if I wanted more control over the lines.

Plot[MapThread[
   Function[{\[Mu], \[Sigma]}, 
    PDF[NormalDistribution[\[Mu], \[Sigma]], x]], {{1, 2, 3, 4, 
     5}, {0.5, 1.0, 1.5, 2.0, 2.5}}] // Evaluate, {x, -10, 10}, 
 Filling -> Axis, 
 PlotLegends -> 
  LineLegend[{"Five Years", "Three Years", "One Year", "Six Months", 
    "Three Months"}], 
 PlotStyle -> {Thickness[.001], Thickness[.002], Thickness[.003], 
   Thickness[.004], Thickness[.005]}, 
 GridLines -> {{1, 2, 3, 4, 5}, None},  
 PlotRange -> All, 
 Epilog -> 
  Point[MapThread[{#1, PDF[NormalDistribution[#1, #2], #1]} &, {{1, 2,
       3, 4, 5}, {0.5, 1.0, 1.5, 2.0, 2.5}}]]]

enter image description here

Show[Plot[
  MapThread[
    Function[{\[Mu], \[Sigma]}, 
     PDF[NormalDistribution[\[Mu], \[Sigma]], x]], {{1, 2, 3, 4, 
      5}, {0.5, 1.0, 1.5, 2.0, 2.5}}] // Evaluate, {x, -10, 10}, 
  Filling -> Axis, 
  PlotLegends -> 
   LineLegend[{"Five Years", "Three Years", "One Year", "Six Months", 
     "Three Months"}], 
  PlotStyle -> {Thickness[.001], Thickness[.002], Thickness[.003], 
    Thickness[.004], Thickness[.005]}, PlotRange -> All],
 ListPlot[
  MapThread[
   Function[{\[Mu], \[Sigma]}, 
    PDF[NormalDistribution[\[Mu], \[Sigma]], \[Mu]]], {{1, 2, 3, 4, 
     5}, {0.5, 1.0, 1.5, 2.0, 2.5}}], Filling -> Bottom
  ]
 ]

enter image description here

| improve this answer | |
$\endgroup$

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.