SmoothDensityHistogram의 MeshFunction

Oct 22 2020

SmoothDensityHistogram특정 (동심) 영역 내부의 확률 수준을 시각화 할 수도있는 데이터 세트 를 만들고 싶습니다 . 다음은 코드입니다.

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]

내가 가진 문제는 각 메쉬 라인으로 둘러싸인 확률을 플롯의 레이블로 직접 표시하는 것입니다 (무엇과 비슷 함 ContourPlot). 조사 MeshFunction중이지만 원하는 결과에 도달 할 수 없습니다. 이상적으로는 플로팅되는 메시 라인도 결정할 수 있습니다 (예 : 60 % 및 80 % 확률에 해당).

편집
나는 또한 여기에 설명 된 접근 방식을 시도했습니다.

SmoothDensityHistogram 위의 등고선

메쉬 라인을 식별하는 데 효과적입니다. 그러나 나는 그들이 둘러싸는 확률 (예제에서 20, 40, 60 및 80 %)에 따라 라벨을 지정하는 방법을 알아낼 수 없습니다.

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

답변

3 JimB Oct 22 2020 at 05:21

주석에있는 두 링크를 결합하면 다음을 수행 할 수 있습니다.

(* 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]}}]]