7
$\begingroup$

I don't like the default amount of padding / white space around BarChart. How can I specify it?

BarChart[{1, 2, 3}]

enter image description here

$\endgroup$
4
  • $\begingroup$ PlotRangePadding->None does't work $\endgroup$ – Tom Jun 16 '20 at 13:10
  • $\begingroup$ You can use AxesOrigin but it seems a little un-intuitive how to specify and I'm not sure how it scales. $\endgroup$ – Tom Jun 16 '20 at 13:11
  • 2
    $\begingroup$ possible duplicate: How to remove the extra space on the left and the right of the default BarChart? $\endgroup$ – kglr Jun 17 '20 at 10:15
  • $\begingroup$ I think you're right. But there have been some neater solutions posted this time :) $\endgroup$ – Tom Jun 17 '20 at 11:32
5
$\begingroup$

This is definitely an example of how vexing it often can be to get flexible graphical output from some built ins in Mathematica, where one is left struggling against the defaults.

A way out may be to force RectangleChart into use, since that function apparently makes much more reasonable choices on plot ranges. RectangleChart needs explicit bar widths, though, so here is a quick hack to add those to a simple dataset:

fakeBarChart = RectangleChart[Transpose@{ConstantArray[1, Length[#]], #}] &;

fakeBarChart[{1, 2, 3}]

rectangle plots

No doubt this will fail messily for more complex datasets, but it may still be easier to fix that than to fight with the silly choices that BarChart makes.

$\endgroup$
1
  • $\begingroup$ This seems like the neatest solution so far. $\endgroup$ – Tom Jun 17 '20 at 8:50
2
$\begingroup$

It's annoying I couldn't find an easy way to do this. You can remove some of the white space if you pad with extra zeros on the right (you can also use PadRight[{1, 2, 3}, 6]) and limit the x-axis PlotRange:

BarChart[{1, 2, 3, 0, 0, 0}, BarSpacing -> 0.1, 
 PlotRange -> {{0, 3.5}, Automatic}]

bar chart padding removed

$\endgroup$
2
$\begingroup$

1. Use the (undocumented) option "FixedBarSpacing" as "FixedBarSpacing" -> True or as Method -> {"FixedBarSpacing" -> True}:

d = Range[3]; 

Row[{BarChart[d, ImageSize -> 300], 
     BarChart[d,  ImageSize -> 300, "FixedBarSpacing" -> True]}, 
  Spacer[10]]

enter image description here

2. Using {d} as the first argument and using the option BarSpacing -> {Automatic, 1} removes the unwanted spaces on the left and right:

BarChart[{d}, BarSpacing -> {Automatic, 1}]

enter image description here

3. Alternatively, a combination of PlotRange + AxesOrigin + DisplayFunction:

BarChart[d, 
 PlotRange -> {{0.4, Length[d] + .5}, All}, 
 PlotRangeClipping -> True, 
 DisplayFunction -> (Show[#, AxesOrigin -> {0.4, 0}] &)]

enter image description here

Note: One would expect using AxesOrigin -> {0.4, 0} directly would have worked. But, somehow, user-specified AxesOrigin option values are overridden.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.