6
$\begingroup$

I have this code:

BarChart[{{Style[0.408808, Black], Style[0.0944349, Purple], 
Style[0.132875, Blue], Style[0.203982, Green], 
Style[0.1599, Red]}, {Style[0.183654, LightRed], 
Style[0.228122, LightGreen], Style[0.164212, LightBlue], 
Style[0.12381, LightPurple], Style[0.300202, Gray]}}, 
BarSpacing -> None, PlotTheme -> "Detailed", 
ChartLabels -> {{Experiment, Theory}, None}, 
LabelStyle -> {18, GrayLevel[0]}]

which produces

enter image description here

How can I hatch the the bars in the Theory part with white lines?

$\endgroup$
3
$\begingroup$

You can do it as follows (using the hack from here Two-color HatchFilling):

ClearAll[wthack]
wthack[clr_:Blue][{d___,h_HatchFilling}]:=PatternFilling[ColorReplace[Graphics[{d,h,Rectangle[]}],White->clr],ImageScaled[1]]
BarChart[
 {{Style[0.408808,Black],
 Style[0.0944349,Purple],
 Style[0.132875,Blue],
 Style[0.203982,Green],
 Style[0.1599,Red]},{
 Style[0.183654,wthack[Opacity[0.5,White]][{Red,HatchFilling[Pi/4,25,30]}]],
 Style[0.228122,wthack[Opacity[0.5,White]][{Green,HatchFilling[Pi/4,25,30]}]],
 Style[0.164212,wthack[Opacity[0.5,White]][{Blue,HatchFilling[Pi/4,25,30]}]],
 Style[0.12381,wthack[Opacity[0.5,White]][{Purple,HatchFilling[Pi/4,25,30]}]],
 Style[0.300202,wthack[Opacity[0.5,White]][{Gray,HatchFilling[Pi/4,25,30]}]]}},
 BarSpacing->None,
 PlotTheme->"Detailed",
 ChartLabels->{{Experiment,Theory},None},
 LabelStyle->{18,GrayLevel[0]}
]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Many thanks! A question: Why does the gray bar on right have an empty space? $\endgroup$ – user74863 Sep 26 at 19:23
  • $\begingroup$ Also, how can I add more white lines? $\endgroup$ – user74863 Sep 26 at 19:27
3
$\begingroup$

In HatchFilling[θ, r, d], the $r$ and $d-r$ is the wide of the filling color and the blank. Here we using $r=10$ and $d-r=4$

Since I don't know how to handle the Light color when we using HatchFilling, I had to set Opacity by hand.

We use Directive to combine the options of HatchFilling and Opacity and color.

fill = HatchFilling[Pi/4, 10, 10 + 4];
BarChart[{{Style[0.408808, Black], Style[0.0944349, Purple], 
   Style[0.132875, Blue], Style[0.203982, Green], 
   Style[0.1599, Red]}, {Style[0.183654, LightRed], 
   Style[0.228122, LightGreen], 
   Style[0.164212, Directive[Opacity[0.1], Blue, fill]], 
   Style[0.12381, Directive[Opacity[0.1], Purple, fill]], 
   Style[0.300202, Directive[Opacity[0.9], Gray, fill]]}}, 
 BarSpacing -> None, PlotTheme -> "Detailed", 
 ChartLabels -> {{Experiment, Theory}, None}, 
 LabelStyle -> {18, GrayLevel[0]}]

enter image description here

| improve this answer | |
$\endgroup$
2
$\begingroup$

you can also use the buiilt-in (but undocumented) chart element function System`BarFunctionDump`TextureBar with your custom textures:

ClearAll[texture]
texture[ms_: {Black, White}, m_: 100] := Texture @ 
  RegionPlot[True, {x, 0, 1}, {y, 0, 1}, 
    MeshFunctions -> {# - #2 &}, Mesh -> m, MeshStyle -> None, 
    MeshShading -> ms, Frame -> False, Axes -> False, 
    PlotRangePadding -> 0, ImagePadding -> 0, BoundaryStyle -> None];

Examples:

data1 = {0.408808, 0.0944349, 0.132875, 0.203982, 0.1599};
data2 = {0.183654, 0.228122, 0.164212, 0.12381, 0.300202};

styles1 = {Black, Purple, Blue, Green, Red};
styles2 =  texture /@ Thread[{White, {LightRed, LightGreen, LightBlue, 
   LightPurple, LightGray}}];

{sdata1, sdata2} = MapThread[Style, #] & /@ { {data1, styles1}, {data2, styles2}};

BarChart[{sdata1, sdata2}, 
 BarSpacing -> None, 
 PlotTheme -> "Detailed", 
 ChartLabels -> {{"Experiment", "Theory"}, None}, 
 LabelStyle -> {18, GrayLevel[0]}, 
 ImageSize -> Large, 
 ChartElementFunction -> System`BarFunctionDump`TextureBar]

enter image description here

Use

styles2b = texture[#, 40] & /@ 
   Thread[{White, {LightRed, LightGreen, LightBlue, LightPurple, LightGray}}];

instead of styles2 to get

enter image description here

Use

styles2c =  texture /@ {{Red, LightRed}, {Green, LightGreen}, {Blue, LightBlue}, 
  {Purple, LightPurple}, {Gray, LightGray}}

to get

enter image description here

See also: This answer to a related Q/A

| improve this answer | |
$\endgroup$

Your Answer

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