MeshFunction ใน SmoothDensityHistogram

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