MeshFunction trong SmoothDensityHistogram
Tôi muốn tạo một SmoothDensityHistogramtập dữ liệu nơi tôi cũng có thể hình dung mức xác suất bên trong các vùng (đồng tâm) nhất định. Đây là mã:
somePoints =
RandomReal[BinormalDistribution[{-2, 2}, {1, 1}, .8], 1000];
SmoothDensityHistogram[somePoints,
Automatic, "PDF",
ColorFunction -> "DarkBands",
BaseStyle -> {FontSize -> 34, FontFamily -> "Arial"},
FrameStyle -> Directive[Black, AbsoluteThickness[3]],
ImageSize -> 800,
AspectRatio -> 0.5,
PlotRange -> All,
MeshStyle -> Black,
Mesh -> 5]
Vấn đề mà tôi gặp phải là chỉ ra xác suất được bao quanh bởi mỗi đường lưới trực tiếp với một nhãn trên biểu đồ (tương tự như những gì ContourPlothiện có). Tôi đang xem xét MeshFunctionnhưng tôi không thể đi đến kết quả mà tôi đang tìm kiếm. Lý tưởng nhất là tôi cũng có thể quyết định các đường lưới được vẽ (ví dụ: tương ứng với xác suất 60% và 80%).
CHỈNH SỬA
Tôi cũng đã thử cách tiếp cận được mô tả ở đây:
Đường viền trên SmoothDensityHistogram
nó hoạt động tốt trong việc xác định các đường lưới. Tuy nhiên, tôi vẫn không thể tìm ra cách gắn nhãn chúng theo xác suất chúng bao quanh (20, 40, 60 và 80% trong ví dụ):
RandomReal[BinormalDistribution[{-2, 2}, {1, 1}, .8], 1000];
d = SmoothKernelDistribution[somePoints];
Show[SmoothDensityHistogram[somePoints,
Automatic, "PDF",
ColorFunction -> "DarkBands",
BaseStyle -> {FontSize -> 34, FontFamily -> "Arial"},
FrameStyle -> Directive[Black, AbsoluteThickness[3]],
ImageSize -> 800,
AspectRatio -> 0.5,
PlotRange -> All,
MeshStyle -> Black,
Mesh -> 0],
ContourPlot[PDF[d, {x, y}], {x, -4, 4}, {y, -5, 5},
PlotRange -> All,
Contours ->
Function[{min, max},
Rescale[{0.2, 0.4, 0.6, 0.8}, {0, 1}, {min, max}]],
ContourShading -> None,
ContourStyle -> {{Black, AbsoluteThickness[3]}}]]
Trả lời
Kết hợp hai liên kết trong các nhận xét, người ta có thể thực hiện những điều sau:
(* Generate some data *)
SeedRandom[12345];
somePoints = RandomVariate[BinormalDistribution[{-2, 2}, {1, 1}, 0.8], 1000];
(* Construct smooth kernel distribution *)
d = SmoothKernelDistribution[somePoints];
(* Find the pdf values on a fine grid and sort by value of pdf *)
pdf = Reverse[Sort[Flatten[Table[PDF[d, {x, y}], {x, -7, 3, 0.05}, {y, -3, 6, 0.05}]]]];
(* Obtain cdf of those values *)
cdf = Accumulate[pdf]/Total[pdf];
(* Give labels for probabilities of interest *)
probabilities = {"0.2", "0.4", "0.6", "0.8"};
(* Determine contours associated with each probability *)
contours = pdf[[Flatten[Table[FirstPosition[cdf, p_ /; p >= alpha], {alpha, ToExpression[probabilities]}]]]];
(* Construct link between the contours and the probability labels along with the desired style of text *)
link = AssociationThread[contours -> probabilities];
f = Text[Style[link[#3], 15, Bold, Red], {#1, #2}] &;
(* Plot results *)
Show[SmoothDensityHistogram[somePoints, Automatic, "PDF", ColorFunction -> "DarkBands",
BaseStyle -> {FontSize -> 34, FontFamily -> "Arial"},
FrameStyle -> Directive[Black, AbsoluteThickness[3]], ImageSize -> 800,
AspectRatio -> 0.5, PlotRange -> All, MeshStyle -> Black, Mesh -> 0],
ContourPlot[PDF[d, {x, y}], {x, -7, 3}, {y, -3, 6}, PlotRange -> All,
Contours -> contours, ContourLabels -> f, ContourShading -> None,
ContourStyle -> {{Black, AbsoluteThickness[3]}}]]