5
$\begingroup$

This question from nearly ten years ago asks how to overlay plots and other graphics-related objects onto a BarChart.

  • When that question was asked (November 2015), the latest version of Mathematica / Wolfram language was 10.3.
  • Today (October 2025), the latest version is 14.3. I'm running 14.0.
  • BarChart has been revised five times since 2015, according to the documentation: in 2017 (v11.1), 2018 (v11.3), 2019 (v12.0), 2021 (v13.0), and 2025 (v14.2).

I'm trying to use the code from this answer by Arnoud Buzing to obtain the tick values from the BarChart created in the question.

Here is the BarChart created in the question:

data = { <|"a" -> 0.70, "b" -> 0.50, "c" -> 0.83, "d" -> 0.23|>, <|"a" -> 0.08, "b" -> 0.58, "c" -> 1.87, "d" -> 1.40|>, <|"a" -> 1.63, "b" -> 1.39, "c" -> 1.10, "d" -> 1.03|>, <|"a" -> 3.22, "b" -> 0.64, "c" -> 0.48, "d" -> 2.38|>, <|"a" -> 3.16, "b" -> 0.37, "c" -> 3.86, "d" -> 0.42|>}; bars = BarChart[data, ChartLegends -> Automatic, PlotRange -> {Automatic, Automatic}] 

BarChart from data

The answer from Arnoud Buzing suggests obtaining the tick values by extracting information about FrameTicks using Options:

ticks = FrameTicks /. Options[First @ bars, FrameTicks] (* {{Automatic, Automatic}, {Automatic, Automatic}} *) 

Unfortunately, as shown above, the output simply references Automatic: {{Automatic, Automatic}, {Automatic, Automatic}}.

What am I doing wrong? How can I extract the "actual" (absolute?) position values of the FrameTicks in bars?

It's possible that I'm making a silly, obvious mistake. If not, then I wonder if Arnoud Buzing's answer no longer works in the same way because of modifications made to BarChart since version 10.

I'm not sure if this is relevant, but I should point out that, as of today (October 2025), Options has been modified once since 2015: in 2024 (v14.1). (But in any case I am running v14.0.) On the other hand, FrameTicks was last modified in 2008 (v7.0).

$\endgroup$
0

1 Answer 1

5
$\begingroup$

You can extract x position of each bar from Rectangle-s.

data = {<|"a" -> 0.70, "b" -> 0.50, "c" -> 0.83, "d" -> 0.23|>, <| "a" -> 0.08, "b" -> 0.58, "c" -> 1.87, "d" -> 1.40|>, <| "a" -> 1.63, "b" -> 1.39, "c" -> 1.10, "d" -> 1.03|>, <| "a" -> 3.22, "b" -> 0.64, "c" -> 0.48, "d" -> 2.38|>, <| "a" -> 3.16, "b" -> 0.37, "c" -> 3.86, "d" -> 0.42|>}; bars = BarChart[data, ChartLegends -> Automatic, PlotRange -> {Automatic, Automatic}]; xx = {#, 0} & /@ Cases[First@bars, Rectangle[x___] :> Mean[{x}[[{1, 2}]]], All][[All, 1]]; Show[bars, Graphics[Point[xx]]] 

enter image description here

So overlaying with ListLinePlot of means would look like this:

data = {<|"a" -> 0.70, "b" -> 0.50, "c" -> 0.83, "d" -> 0.23|>, <| "a" -> 0.08, "b" -> 0.58, "c" -> 1.87, "d" -> 1.40|>, <| "a" -> 1.63, "b" -> 1.39, "c" -> 1.10, "d" -> 1.03|>, <| "a" -> 3.22, "b" -> 0.64, "c" -> 0.48, "d" -> 2.38|>, <| "a" -> 3.16, "b" -> 0.37, "c" -> 3.86, "d" -> 0.42|>}; bars = BarChart[data, ChartLegends -> Automatic, PlotRange -> {Automatic, Automatic}]; xx = Mean /@ Partition[ Cases[First@bars, Rectangle[x___] :> {1, 2}*Mean[{x}[[{1, 2}]]], All], Length[data[[1]]]]; Show[bars, ListLinePlot[xx, Mesh -> All]] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.