2
$\begingroup$

I am trying to make those downward "tick marks" below the horizontal axis of a BarChart shorter or removed. They look very crowded in a figure like the following one. How can I do that?

enter image description here

Here is the code to produce the figure:

n = Range[10];
dat1 = 1/n^2;
dat2 = 0.9 dat1;
dat = {dat1, dat2};
sty = {Orange, Black};

SetOptions[BarChart, 
  BaseStyle -> {FontFamily -> "Times New Roman", FontSize -> 8}, 
  AxesStyle -> AbsoluteThickness[0.5], LabelStyle -> Black];
SetOptions[Labeled, 
  LabelStyle -> {FontFamily -> "Times New Roman", FontSize -> 8}];

pic = Labeled[
  BarChart[Transpose@dat, ChartStyle -> {Orange, Black}, 
   BarSpacing -> {0, 1}, 
   ChartLabels -> {Placed[n, Above, Style[#, FontSize -> 8] &], None},
    AspectRatio -> 1/2, ImageSize -> 225], {"Horizontal Axis", 
   "Vertical Axis"}, {Bottom, Left}, RotateLabel -> True]
$\endgroup$
2
$\begingroup$

It looks like the tick lengths are hard-coded, so the only way to fix the lengths is to post-process the graphics output. Since the ticks have the form Line[{a, Offset[b, c]}], you can use:

pic /. Line[{a_, Offset[b_, c_]}] :> Line[{a, Offset[b/2, c]}]

enter image description here

to cut the length of the ticks in half. If you want to remove them entirely, you could use:

pic /. Line[{a_, Offset[b_, c_]}] :> Sequence[]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, @Carl Woll. This works perfect. Can you explain a little bit what are the parameters a, b, and c in Line[{a, Offset[b, c]}]? To remove the ticks, why Sequence[] is needed instead of just changing b to 0? $\endgroup$
    – nanjun
    May 24 '19 at 20:23
1
$\begingroup$

This is not precisely an answer to the question. I find BarChart to be particularly unwieldy. It's not that much trouble to reproduce the figure using Graphics, allowing easy control of all the elements.

Show[
 Plot[{}, {x, 0, Length[dat1]}, PlotRange -> {0, Max[dat] + .1}],
 Graphics[{
   Table[{Orange, EdgeForm[Black], Rectangle[{n - 1, 0}, {n - .6, dat1[[n]]}], 
          Black, Rectangle[{n - .6, 0}, {n - .2, dat2[[n]]}]}, {n, Length[dat1]}],
   Table[Text[n, {n - .6, Max[Transpose[dat][[n]]] + .05}], {n, Length[dat1]}]
   }], 
 AspectRatio -> .5, 
 BaseStyle -> {FontFamily -> "Times New Roman", FontSize -> 10}, 
 Frame -> {True, True, False, False}, 
 FrameTicks -> {None, Automatic},
 FrameLabel -> {"Horizontal Axis", "Vertical Axis"}]

enter image description here

$\endgroup$
1
$\begingroup$

Those marks aren't ticks; they are data set grouping indicators. They are intended to help the viewer associate the label with data group that it labels. I do not know how to eliminate them, but certain plot themes draw them above the x-axis rather than below. You may find that more acceptable.

For example:

Labeled[
  BarChart[Transpose@dat,
    PlotTheme -> "Web",
    BaseStyle -> {"TR", 8},
    ChartStyle -> {Orange, Black},
    BarSpacing -> {0, 1}, 
    ChartLabels -> {Placed[n, Above, Style[#, FontSize -> 8] &], None}, 
    AspectRatio -> 1/2,
    ImageSize -> 300],
  Style[#, "TR", 10] & /@ {"Horizontal Axis", "Vertical Axis"},
  {Bottom, Left},
  RotateLabel -> True]

plot

$\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.