3
$\begingroup$

I am making a plot with SmoothDensityHistogram that looks like this:

SmoothDensityHistogram[RandomVariate[BinormalDistribution[.5], 10],MeshStyle -> Red, Mesh -> 3, PlotLegends ->Placed[BarLegend[Automatic,LabelStyle -> {GrayLevel[0.3], 30, FontFamily -> "Arial"}],   After]]

I would need some help on the following: - I would like to decide which Mesh lines are shown and indicate their value on it; - in the BarLegend, multiply all the values that are shown by a given number or, equivalently, normalize the scale to be between 0 and 1.

thanks!

$\endgroup$
3
$\begingroup$

One way to get more control is to build your own version of SmoothDensityHistogram using DensityPlot and ContourPlot.

data = RandomVariate[BinormalDistribution[.5], 10];
pdf = PDF[SmoothKernelDistribution[data], {x, y}];

dp = DensityPlot[
   pdf,
   {x, -3, 3},
   {y, -3, 3},
   PlotLegends -> Automatic
   ];

cp = ContourPlot[
   pdf,
   {x, -3, 3},
   {y, -3, 3},
   Contours -> 4,
   ContourShading -> None,
   ContourLabels -> True
   ];

Show[dp, cp]

Plot example

To multiply all values by a number, you can multiply the PDF by that number.

The functions have options that can help you achieve higher quality in the figure. DensityPlot has an option called PlotPoints for example.

| improve this answer | |
$\endgroup$
2
$\begingroup$
SeedRandom[1]
data = RandomVariate[BinormalDistribution[.5], 10];
pdf = PDF[SmoothKernelDistribution[data]];

sdh = SmoothDensityHistogram[data, MeshStyle -> Red, Mesh -> 3, 
   Method -> {"BoundaryOffset" -> False}, 
   PlotLegends -> Placed[BarLegend[Automatic, 
      LabelStyle -> {GrayLevel[0.3], 30, FontFamily -> "Arial"}], After]];

Extract the mesh lines from sdh and find the associated pdf values:

mesh = DeleteDuplicates @ Round[Cases[Normal@sdh, Line[x_] :> pdf[x[[1]]], All], .001];

Post-process sdh to inject the (undocumented) sub-option "StyledContours" into the legend:

styledcontours = Method -> "StyledContours" -> Thread[{mesh, Directive[Thick, Red]}];

sdh /. b_BarLegend :> Append[b, styledcontours]

enter image description here

To add text labels to the mesh lines using another round of post-processing:

epilog = Cases[Normal @ sdh, Line[x_] :> 
    Text[ Round[pdf[x[[1]]], .001], BSplineFunction[x]@.5], All];

Show[sdh /. b_BarLegend :> Append[b, styledcontours], Epilog -> epilog]

enter image description here

Alternatively, we can construct a ContourPlot using mesh as Contours and the option ContourLabels and Show it with sdh:

cp = ContourPlot[pdf[{x, y}],, {x, -1, 2}, {y, -3, 3}, 
   Contours -> DeleteDuplicatesBy[Round[#, .01] &]@mesh, 
   ContourStyle -> Red, ContourLabels -> All, ContourShading -> None, 
   PlotRange -> All];

Show[sdh /. b_BarLegend :> Append[b, styledcontours], cp]

enter image description here

You can also change the list of labeled ticks in BarLegend using

Show[sdh /. b_BarLegend :> Append[b, {styledcontours, 
     "Ticks" -> DeleteDuplicatesBy[Round[#, .01] &]@mesh}],
  cp]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.