2011-12-19 38 views
11

我可以在單個繪圖內指定不同的填充顏色,如波紋管還是需要「顯示」幾個繪圖?假設我希望填充樣式與PlotStyle相同。在Mathematica中使用單個繪圖的填充樣式

priorMean = 50; 
priorVar = 100; 

llhMean = 30; 
llhVar = 40; 

postMean=35.71; 
postVar=28.57; 


Plot[ 
    [email protected][ 
    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

+1

不'FillingStyle'你想要做什麼? – Verbeia 2011-12-19 20:08:12

+0

500我很好奇:我以爲你會喜歡我的回答,但沒有評論。它不適用於您的應用程序嗎? – 2011-12-21 02:50:19

+0

@Mr。它是 !我有這個截止日期,使我無法深入研究它。但我實際上正在考慮將此與其他兩種關於Graphics的解決方案相結合,這代表了一種很好的設定「在岩石中」的理念,我只需要向您提問就可以了!但我喜歡它! – 500 2011-12-21 03:00:01

回答

13

你需要使用FillingStyle填寫,我覺得你陷入了語法FillingStyle ,這是不是與相同,雖然你會期望它。你必須指定一個顏色爲每條曲線爲FillingStyle -> {1 -> color1, 2 -> color2},等這裏有一個例子:

colors = {Red, Green, Blue}; 
Plot[[email protected] 
    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

8

你可以做類似

With[{colours = {Red, Green, Blue}}, 
Plot[[email protected] 
    MapThread[ 
    Function[{\[Mu], \[Sigma]}, 
    PDF[NormalDistribution[\[Mu], Sqrt[\[Sigma]]], x]], 
    {{priorMean, llhMean, postMean}, {priorVar, llhVar, postVar}}], 
    {x, 0, 100}, 
    Filling -> 
    MapIndexed[#2[[1]] -> {Axis, Directive[Opacity[.3, #1]]} &, colours], 
    PlotStyle -> colours]] 

filling with different colours

3

這得到一個結果:

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

發現在FillingStyle下幫助,範圍,填充樣式。

還或者:

f = MapThread[ 
    Function[{\[Mu], \[Sigma]}, 
    PDF[NormalDistribution[\[Mu], Sqrt[\[Sigma]]], x]], 
    {{priorMean, llhMean, postMean}, {priorVar, llhVar, postVar}}]; 
c = {Red, Green, Blue}; 
Show[Array[ 
    Plot[f[[#]], {x, 0, 100}, Filling -> {1 -> {Axis, c[[#]]}}, 
    PlotRange -> {Automatic, 0.08}, PlotStyle -> c[[#]]] &, 3]] 

enter image description here

9

我建議做一個擴展的Plot定義。 I have done this before.

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

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

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

有了這個地方,你可以使用FillingStyle -> "Match"自動式的填充相匹配的款式爲主。

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

Mathematica graphics

+1

我喜歡這種方法。 – abcd 2011-12-20 00:22:12

+0

@yoda,謝謝 – 2011-12-20 00:22:40