Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling Styles using a single Plot in Mathematica

Could I specify different filling colors for within a single plot like the bellow or would I need to "Show" several Plots ? Let`s say I would like the filling style to be the same as the PlotStyle.

priorMean = 50;
priorVar = 100;

llhMean = 30;
llhVar = 40;

postMean=35.71;
postVar=28.57;


Plot[
     Evaluate@MapThread[
     Function[{\[Mu], \[Sigma]},
     PDF[NormalDistribution[\[Mu], Sqrt[\[Sigma]]], x]],
     {{priorMean, llhMean, postMean}, {priorVar, llhVar, postVar}}],
{x, 0, 100}, Filling -> Axis, PlotStyle -> {Red, Green, Blue}]

enter image description here

like image 891
500 Avatar asked Dec 19 '11 19:12

500


People also ask

What is the use of plot style in Mathematica?

is an option for plotting and related functions that specifies styles in which objects are to be drawn.


2 Answers

You'll need to use FillingStyle to fill in. I think you got stuck in the syntax for FillingStyle, which is not the same as that for PlotStyle, although you'd expect it to be. You'll have to assign a color for each curve as FillingStyle -> {1 -> color1, 2 -> color2}, etc. Here's an example:

colors = {Red, Green, Blue};
Plot[Evaluate@
  MapThread[
   Function[{\[Mu], \[Sigma]}, 
    PDF[NormalDistribution[\[Mu], Sqrt[\[Sigma]]], x]], {{priorMean, 
     llhMean, postMean}, {priorVar, llhVar, postVar}}], {x, 0, 100}, 
 Filling -> Axis, PlotStyle -> colors, 
 FillingStyle -> 
  MapIndexed[#2 -> Directive[Opacity[0.3], #] &, colors]]

enter image description here

like image 51
abcd Avatar answered Oct 30 '22 20:10

abcd


I propose making an extension to the definition of Plot. I have done this before.

toDirective[{ps__} | ps__] := Flatten[Directive @@ Flatten[{#}]] & /@ {ps}

makefills = MapIndexed[#2 -> Join @@ toDirective@{Opacity[0.3], #} &, #] &;

Unprotect[Plot];
Plot[a__, b : OptionsPattern[]] :=
  Block[{$FSmatch = True},
    With[{fills = makefills@OptionValue[PlotStyle]}, 
      Plot[a, FillingStyle -> fills, b]
  ]] /; ! TrueQ[$FSmatch] /; OptionValue[FillingStyle] === "Match"

With this in place, you can use FillingStyle -> "Match" to auto-style the fills to match the main styles.

Plot[{Sin[x], Cos[x], Log[x]}, {x, 0, 2 Pi},
  PlotRange -> {-2, 2},
  PlotStyle -> {{Blue, Dashing[{0.04, 0.01}]},
                {Thick, Dashed, Orange},
                {Darker@Green, Thick}},
  Filling -> Axis,
  FillingStyle -> "Match"
]

Mathematica graphics

like image 37
Mr.Wizard Avatar answered Oct 30 '22 19:10

Mr.Wizard