Skip to main content

plotting - How do I plot a histogram with hatched shading?


I can plot an ordinary histogram with blue shading and black borders like this:



T = RandomVariate[NormalDistribution[0, 1], 10000];
Histogram[T, 30]

And I can simulate hatching like this:


T = RandomVariate[NormalDistribution[0, 1], 10000];
Histogram[T, 30, ChartElements -> Graphics[{Black, Line[{{0, 0}, {1, 1}}]}]]

How can I get the hatching and the black borders at the same time?



Answer



I was sure Histogram can be modified to have the hatching style. Little late but what about this!



g[{{xmin_, xmax_}, {ymin_, ymax_}}, ___] := Module[{yval, line},
yval = Range[ymin, ymax, 15];
line = Line /@ Transpose@{Most@({xmin, #} & /@ yval),Rest@({xmax, #} & /@ yval)};
{FaceForm[White],Polygon[{{xmin, ymin}, {xmax, ymin}, {xmax, ymax}, {xmin, ymax}}],
Orange, line}];
T = RandomVariate[NormalDistribution[0, 1], 10000];
Histogram[T, 30, ChartElementFunction -> g,
ChartBaseStyle -> EdgeForm[{Thin, Darker@Orange}], Frame -> True]

enter image description here



Check in the function where yval is defined with Range[ymin, ymax, 15] one can change the $15$ to change the amount of hatching. You also have total control of the Graphics primitive used in the ChartElementFunction so you can use many more Directive for example Opacity and all.


BR


Comments