# Filling smoothed histogram with area representing standard deviation?

I've done a cluster analysis on a set of datapoints (listV) and used Mathematica to obtain a smoothed histogram with the x and y axes inverted. To aid visualisation I would like to fill the area underneath the histogram towards the y axis beginning at a value for each histogram determined from the earlier cluster analysis (lets call this A1), with width equal to the standard deviation determined from the analysis as well (A1 ± S1; shown in listA). There is a crude cartoon (Sorry - Paint!) of how it would look like next to a plot of the clusters in question. Due to inverting the x/y axis the Filling command does not seem to work.

My question is if it would be possible to fill this area for all four datasets in the plot?

I've also tried RegionPlot but have not managed to get it to work as yet (and am still trying!). A shortened version of the code is shown below. Thanks!

listA={{A1, S1},{A2, S2},{A3, S3},{A4, S4}}
listV = {data1[[All, 2]], data2[[All, 2]], data3[[All, 2]], data4[[All, 2]]};
sHV = SmoothHistogram[listV, 0.881, "PDF",
PlotRange -> {Automatic, Automatic},
ImageSize -> {500, 300},
PlotRange -> {1, 2.5},
];
plotSHV =Show[sHV /. x_Line :> Reverse[x, 3], PlotRange -> {Automatic, {1, 2.5}}, AxesOrigin -> {0, 1}]


What was done in the end:

Thank you for the answers. After working a bit more on it I've used the answers below in combination with Jens' function here, so that the smoothed histogram plots are stacked and look less cluttered. I'm pretty happy so far with the result. I've included the code below. Probably (definitely) not the cleanest, but I hope it helps anyone with a similar problem. plots1 = {{
Show[
{
SmoothHistogram[data1[[All, 2]], hData1, "PDF"],
SmoothHistogram[data1[[All, 2]], hData1, "PDF",
PlotRange -> {{2, 9}, {0, 0.5}}, Filling -> Axis,
FillingStyle -> Opacity[0.75, data1Colour],
RegionFunction ->
Function[{x, y}, data1shadeMin < x <data1shadeMax]]
}
/.
{
x_Line:>GeometricTransformation[Rotate[x, -Pi/2, {0, 0}],ReflectionTransform[{0, 1}]],
x_Polygon:>GeometricTransformation[Rotate[x, -Pi/2, {0, 0}],ReflectionTransform[{0, 1}]]
},
plotSettings,
PlotRange -> {Automatic, {2, 9}},
AxesOrigin -> {0, 2}
],
(*Other plots are omitted, they are just a repeat of this one.*)
}}

plotSHdata1 = Show
[
Rasterize[Grid[{{plotdata1, plotGrid[plots1, 400, 300]}}]],
Graphics[{Thick, Black,
Line[{ImageScaled[{0.3, 0.62}], ImageScaled[{0.96, 0.62}]}]}],
Graphics[{Thick, Black,
Line[{ImageScaled[{0.15, 0.495}], ImageScaled[{0.67, 0.495}]}]}],
Graphics[{Thick, Black,
Line[{ImageScaled[{0.16, 0.48}], ImageScaled[{0.774, 0.48}]}]}],
Graphics[{Thick, Black,
Line[{ImageScaled[{0.29, 0.54}], ImageScaled[{0.874, 0.54}]}]}],

Epilog -> {
Inset[
SwatchLegend[{Orange, Green, Blue, Red}, {1, 2, 3, 4},
LegendLayout -> "Row", LegendFunction -> Framed,
LabelStyle -> {FontSize -> 17}, LegendMarkerSize -> {12}]
, ImageScaled[{0.78, 0.063}]
],
Style[{Text["label", ImageScaled[{0.585, 0.97}]]}, labelColour,
labelFont],
Style[{Text["Probability Density Function (PDF)",
ImageScaled[{0.78, 0.16}]]}, labelColour, labelFont]
}
]

• There seems to be two issues: (1) How to fill and (2) What to fill. The "What" is not clear from your statement "the area underneath the histogram towards the y axis with a width that represents the data's standard deviation." You could fill the central 68.27% of the area but that only has a logical connection to a standard deviation for a normal distribution. And all of that filling would make for a pretty crowded figure. – JimB Mar 29 '16 at 18:24
• Hi. Thanks for the reply. I've edited some additional information into the question, and also included a sketch (Paint so a little crude - Sorry!) to give an idea of what area it would be. Yes, it would seem that the figure will be quite crowded - was hoping that a hatched fill would alleviate that and/or figuring out alternatives. – Zhao Mar 29 '16 at 18:57

## 2 Answers

s = ColorData;
d = Table[RandomVariate[NormalDistribution[c, 2], 500], {c, 0, 9, 3}];
(*filling Mean ± Std Dev.You may chose other limits*)
lims = MapThread[({#1 - #2, #1 + #2}) &, {Mean /@ d, StandardDeviation /@ d}];

dd = Transpose[{d, lims}];
i = 1;
f[x_] := GeometricTransformation[Rotate[x, -Pi/2, {0, 0}], ReflectionTransform[{0, 1}]]

sHV = Show[{SmoothHistogram[#[], PlotStyle-> s@i, PlotRange-> {{-6, 15}, Automatic}],
SmoothHistogram[#[], PlotStyle-> s@i, Filling -> Axis,
RegionFunction -> Function[{x, y}, #[[2, 1]] < x < #[[2, 2]]],
FillingStyle -> Directive[Opacity[.2], s[i++]]]} & /@ dd];

Show[sHV/. x_Polygon | x_Line:> f@x,
PlotRange -> Reverse[PlotRange /. AbsoluteOptions[sHV, PlotRange]],
AspectRatio-> 2] • Thanks! I didn't realise/know that the RegionFunction could be used to limit the plot range. – Zhao Mar 30 '16 at 19:01
• @Zhao It is a nice trick to use when you need two different plotting styles in different regions for the same curve – Dr. belisarius Mar 30 '16 at 19:03

Define a function using a combination of the options Mesh, MeshShading and ColorFunction to get partially filled histograms:

shF = SmoothHistogram[#, Filling -> Axis,
Mesh -> {#2}, MeshStyle -> Transparent, MeshShading -> {#3, Directive[Thick,#3]},
ColorFunctionScaling -> False,
ColorFunction->Function[{x, y}, If[#2[]<=x<=#2[], Opacity[.4, #3], Transparent]]]&;


and a function to flip the axes by post-processing to reverse the elements in the first argument of GraphicsComplex:

flipF = # /.  GraphicsComplex[p_, x__] :> GraphicsComplex[Reverse /@ p, x] &;


Using an input setup similar to belisarius's:

d = Table[RandomVariate[NormalDistribution[c, 2], 500], {c, 0, 6, 2}];
lims =({#1 - #2, #1 + #2}) & @@@ Thread[{Mean /@ d, StandardDeviation /@ d}];
colors = ColorData[1, "ColorList"][[;; 4]];
ddd = Transpose[{d, lims, colors}];

Show[flipF@Show[shF @@@ ddd], AspectRatio -> 2, PlotRange -> All] 