求问一个思路,这样的单变量边缘分布直方图怎么用mma画?
mathematica吧
全部回复
仅看楼主
level 6
我的思路是将直方图经ImageRotation旋转后,再通过GraphaGrid拼接在一起。
不知道是否存在内置的函数,能直接画出来这样的图?
2023年11月13日 03点11分 1
level 6
我知道这个怎么搞了。搞定后可以分享在这里。
2023年11月14日 02点11分 4
level 6
第一种Mathematica内置的函数,转自Stack Exchange
data = RandomReal[BinormalDistribution[{-1, 2}, {1, 1}, .8], 1000];
GraphicsColumn[
Table[DensityHistogram[data, {.2}, ColorFunction -> "DarkRainbow",
Method -> {"DistributionAxes" -> p}, ImageSize -> 500,
BaseStyle -> {FontFamily -> "Helvetica"},
LabelStyle -> Bold], {p, {True, "Histogram", "SmoothHistogram",
"BoxWhisker"}}]]
2023年11月14日 05点11分 5
level 6
第二种,用Grid拼接
data = RandomVariate[BinormalDistribution[0.5], 1000];
edist = EstimatedDistribution[data,
BinormalDistribution[{\[Mu]1, \[Mu]2}, {\[Sigma]1, \[Sigma]2}, \
\[Rho]]]
fontsize = 20;
main = ListPlot[data, PlotRange -> {{-4, 4}, {-4, 4}}, Frame -> True,
PlotStyle -> PointSize[0.01],
FrameLabel -> {Style["Frequency deviation (\[Sigma])", fontsize,
FontFamily -> "Times"],
Style["Amplitude deviation (\[Sigma])", fontsize,
FontFamily -> "Times"]}, ImageSize -> {600, Automatic},
AspectRatio -> 1,
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> 16, Thick, FontFamily -> "Times"],
ImagePadding -> {{All, None}, {All, None}}]
yhist = Show[
ParametricPlot[{PDF[MarginalDistribution[edist, 2], y], y}, {y, -4,
4}, PlotStyle -> Red],
Histogram[Transpose[data][[2]], Automatic, "PDF",
BarOrigin -> Left], FrameTicks -> {All, None, None, All},
Frame -> {True, True, True, True}, ImageSize -> {Automatic, 600},
PlotRange -> {All, {-4, 4}}, Axes -> None,
FrameLabel -> {Style["Probabilty density", fontsize,
FontFamily -> "Times"], None, None, None}, AspectRatio -> 3,
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> 16, Thick, FontFamily -> "Times"],
ImagePadding -> {{1, All}, {All, None}}]
xhist = Show[
Plot[PDF[MarginalDistribution[edist, 1], x], {x, -4, 4},
PlotStyle -> Red],
Histogram[Transpose[data][[1]], Automatic, "PDF"], Axes -> None,
FrameTicks -> {None, All, All, None}, Frame -> True,
FrameLabel -> {None,
Style["Probabilty density", fontsize, FontFamily -> "Times"]},
ImageSize -> {600, Automatic}, AspectRatio -> 1/3,
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> 16, Thick, FontFamily -> "Times"],
PlotRange -> {{-4, 4}, All}(*,ImagePadding->{{All,None},{None,All}}*)]
fig = Grid[{{xhist}, {main, yhist}}, Spacings -> {0, 0}]
2023年11月14日 05点11分 6
level 6
第三种,用Inset。参考自Stack Exchange,本人略微修改。
Clear[customPlot]
fontsize = 15;
customPlot[data_, edist_, o___] :=
Block[{xmin, xmax, ymin, ymax, x, y, mainplot, xhist, yhist,
opts = Flatten[{o}]}, {x, y} = Transpose[data];
xhist = HistogramList[x, 50];
yhist = HistogramList[y, 50];
{xmin, xmax} = Through[{Min, Max}[First[xhist]]];
{ymin, ymax} = Through[{Min, Max}[First[yhist]]];
mainplot =
ListPlot[data, Frame -> {True, True, True, True}, Axes -> False,
FrameTicks -> Automatic, AspectRatio -> 1,
PlotRange -> {{xmin, xmax}, {ymin, ymax}},(*PlotRangePadding->
Scaled[0.02],*)PlotStyle -> PointSize[0.01],
FrameLabel -> {Style["Frequency deviation (\[Sigma])", fontsize,
FontFamily -> "Times"],
Style["Amplitude deviation (\[Sigma])", fontsize,
FontFamily -> "Times"]},
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> fontsize, Thick, FontFamily -> "Times"],
FilterRules[opts, Options[ListPlot]], GridLines -> Automatic,
GridLinesStyle -> Directive[Gray, Dotted]];
xhist =
Show[Plot[PDF[MarginalDistribution[edist, 1], x], {x, xmin, xmax},
PlotStyle -> Red],
Histogram[x, {First[xhist]}, "PDF",
FilterRules[opts, Options[Histogram]]], Axes -> None,
Frame -> {{True, True}, {True, True}},
FrameTicks -> {{Automatic, None}, {None, Automatic}},
ImagePadding -> {{All, None}, {None, All}},
GridLines -> {None, None},
FrameLabel -> {None,
Style["Probabilty density", fontsize, FontFamily -> "Times"]},
ImageSize -> {600, Automatic}, AspectRatio -> 1/3,
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> fontsize, Thick, FontFamily -> "Times"],
GridLinesStyle -> Directive[Gray, Dotted], PlotRange -> All];
yhist =
Show[ParametricPlot[{PDF[MarginalDistribution[edist, 1], y],
y}, {y, ymin, ymax}, PlotStyle -> Red],
Histogram[y, {First[yhist]}, "PDF", BarOrigin -> Left,
FilterRules[opts, Options[Histogram]]], Axes -> None,
Frame -> {{True, True}, {True, True}},
FrameTicks -> {{None, Automatic}, {Automatic, None}},
ImagePadding -> {{None, All}, {All, None}},
GridLines -> {None, None},
FrameLabel -> {Style["Probabilty density", fontsize,
FontFamily -> "Times"], None, None, None}, AspectRatio -> 3,
FrameStyle -> Directive[Black, Thick, FontFamily -> "Times"],
PlotTheme -> "Scientific",
FrameTicksStyle ->
Directive[FontSize -> fontsize, Thick, FontFamily -> "Times"],
GridLinesStyle -> Directive[Gray, Dotted]];
Graphics[{{Opacity[0], Point[{{-900, -900}, {300, 300}}]},
Inset[mainplot, {0, 0}, {Right, Top}, {-900, -900}],
Inset[xhist, {0, 0}, {Right, Bottom}, {-900, Automatic}],
Inset[yhist, {0, 0}, {Left, Top}, {Automatic, -900}]},
PlotRange -> {{-900, 300}, {-900, 300}},
FilterRules[opts, Options[Graphics]],
ImagePadding -> {{1, 1}, {1, 1}}, ImageSize -> 600]]
data = RandomVariate[BinormalDistribution[0.5], 1000];
edist = EstimatedDistribution[data,
BinormalDistribution[{\[Mu]1, \[Mu]2}, {\[Sigma]1, \[Sigma]2}, \
\[Rho]]];
fig = customPlot[data, edist]
2023年11月14日 05点11分 7
level 6
因为要求字体等格式,所以加了很多额外的函数,发出来就很长了[哈哈]
2023年11月14日 05点11分 8
level 6
2023年11月14日 05点11分 9
吧务
level 15
stackexchange的相应链接:
mathematica.stackexchange.com/q/2984/1871
2023年12月02日 04点12分 10
1