在 Mathematica 中使用单个图填充样式

发布于 2024-12-21 18:35:16 字数 514 浏览 2 评论 0原文

我可以在如下所示的单个图中指定不同的填充颜色,还是需要“显示”多个图?假设我希望填充样式与 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}]

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戴着白色围巾的女孩 2024-12-28 18:35:16

您需要使用 FillingStyle 来填写。我认为您陷入了 FillingStyle 的语法,它与 FillingStyle 不同。对于 PlotStyle,尽管您希望如此。您必须为每条曲线指定一种颜色 FillingStyle -> {1->颜色1, 2 -> color2} 等。这是一个示例:

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]]

在此处输入图像描述

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

萌无敌 2024-12-28 18:35:16

我建议对 Plot 的定义进行扩展。 我之前已经这样做过。

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"

完成此操作后,您可以使用 FillingStyle -> “匹配” 自动设置填充样式以匹配主要样式。

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 图形

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

坦然微笑 2024-12-28 18:35:16

你可以做类似

With[{colours = {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 -> 
    MapIndexed[#2[[1]] -> {Axis, Directive[Opacity[.3, #1]]} &, colours], 
  PlotStyle -> colours]]

填充不同颜色

You could do something like

With[{colours = {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 -> 
    MapIndexed[#2[[1]] -> {Axis, Directive[Opacity[.3, #1]]} &, colours], 
  PlotStyle -> colours]]

filling with different colours

心安伴我暖 2024-12-28 18:35:16

得到的结果是:

Plot[Evaluate@
  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、Scope、Filling Style 下找到。

或者:

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]]

在此处输入图像描述

This gets a result:

Plot[Evaluate@
  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}]

Found in the help under FillingStyle, Scope, Filling Style.

And alternatively:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文