To Work with Barchart or DiscretePlot?
This is a bit easier to achieve with DiscretePlot
instead of BarChart
.
With
f[x_] := 3 - 1/2 x
Then
Show[
Plot[f[x], {x, 0, 14}],
DiscretePlot[f[x], {x, Range[2, 12, 2]},
PlotMarkers -> "Point",
ExtentSize -> Right,
PlotStyle -> Gray],
Epilog -> {
Inset[Row[{Inactivate[f[x]], "=", f[x]}, Spacer[1]],
Scaled[{.2, .2}],
FormatType -> TraditionalForm,
BaseStyle -> {FontSize -> Scaled[.04]}]
}
]
Hope this helps.
Update
Taking cues from this answer (19542) and applying to a custom ExtentElementFunction
can be created.
ClearAll[fillRandomDots];
fillRandomDots[{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
Module[{rect = Rectangle[{xmin, ymin}, {xmax, ymax}], dots, texture},
If[ymax - ymin > 0,
dots = RandomPoint[rect, Area@rect 600];
texture =
Rasterize@
Graphics[{Opacity[.1, Gray], rect,
Opacity[.9, Lighter[Black, .3]], Disk[#, .05] & /@ dots},
PlotRange -> {{xmin, xmax}, {ymin, ymax}},
PlotRangePadding -> None,
ImagePadding -> None
];
,
texture = Graphics@{}
];
{
EdgeForm[{Thin, Black}],
Texture@texture,
Polygon[{{xmin, ymin}, {xmax, ymin}, {xmax, ymax}, {xmin, ymax}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]
}
]
Then
Show[
DiscretePlot[f[x], {x, Range[2, 12, 2]},
PlotMarkers -> "Point",
ExtentSize -> Right,
ExtentElementFunction -> fillRandomDots],
Plot[f[x], {x, 0, 14},
PlotTheme -> "Monochrome"],
PlotRange -> {{0, 14}, {-4, 3}},
AxesOrigin -> {0, 0},
Epilog -> {
Inset[Row[{Inactivate[f[x]], "=", f[x]}, Spacer[1]],
Scaled[{.2, .2}],
FormatType -> TraditionalForm,
BaseStyle -> {FontSize -> Scaled[.04]}]
}
]
Cues can be taken from this answer (19542) to change the sides the axis ticks and labels appear.
You can also use RectangleChart with a custom ChartElementFunction:
ClearAll[ceF, rectChart]
ceF[func_][{{x0_, x1_}, {y0_, y1_}}, ___] :=
Dynamic@{If[CurrentValue["Color"]=== White, {},
{Texture[Rasterize[RandomImage[1, {50, 50}]]],
Polygon[#, VertexTextureCoordinates -> #]&@{{x0, y0}, {x0, y1}, {x1, y1}, {x1, y0}}}],
Text[Style[Floor@x0, If[Floor@x0 == 0, White, Black], "Panel", 12], {x0, 0},
Switch[Sign[func /@ {x0 - .01 Mean[{x0, x1}], x0 + .01 Mean[{x0, x1}]}],
{1, 1} | {1, -1} | {0, -1}, {0, 1}, {-1, -1} | {-1, 1} | {0, 1}, {0, -1}]]};
rectChart[fun_, from_, to_, o : OptionsPattern[]] :=
RectangleChart[ArrayPad[Table[{from, fun[x]}, {x, from, to - 1, from}], {{1}},
Style[{from, 1}, White]] /. x : {_, 0} :> Style[x + {0, 1}, White],
BarSpacing -> 0, o,
Epilog -> (Plot[fun[x], {x, 0, to + from}, PlotStyle -> GrayLevel[.1]][[1]]),
PlotRange -> {fun[0], fun[to]}, ChartElementFunction -> ceF[fun]];
Examples:
f[x_] := 3 - x/2;
rectChart[f, 2, 14, ChartLegends ->
Placed[TraditionalForm[Style[HoldForm@f[x] == f[x], 16, "Panel"]], {.2, .3}]]
f[x_] := 3 - x/2;
rectChart[f, 1, 14, ChartLegends ->
Placed[TraditionalForm[Style[HoldForm@f[x] == f[x], 16, "Panel"]], {.2, .3}]]
rectChart[# Pi Cos[# Pi/4]/4 &, 2, 21, PlotRange -> {-20, 20},
ChartLegends -> Placed[TraditionalForm[
Style[HoldForm@f[x] == x Pi Cos[x Pi/4]/4, 16, "Panel"]], {.3, .9}]]