第三种,用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]