Message Boards Message Boards

6 Replies
1 Total Likes
View groups...
Share this post:

How Can I Change the Alignment of Histogram Bin Tick Labels?

Posted 9 years ago

I'm building a Histogram that will eventually have integer data that looks approximately like this (but not generated randomly):

data = RandomInteger[{1, 5}, 100];


enter image description here

I want the X-axis labels to be centred under the bin bars, because, for example, that first left-hand bar really is the count of 1's; there are no values between 1 and 2, and, looking to the right-hand side, there certainly are no values of 6.

How can I align the X-axis tick labels so they are centred under each bar?

Thanks in advance


POSTED BY: Brad Varey
6 Replies
Posted 9 years ago


That is absolutely perfect!

Thanks very much,


POSTED BY: Brad Varey
Posted 9 years ago

How about this:

data = RandomInteger[{1, 5}, 100];
Histogram[data, {0.5, Max[data] + 0.5, 1}, 
 Ticks -> {Table[x, {x, Min[data], Max[data], 1}], Automatic}]


POSTED BY: Jim Baldwin
Posted 9 years ago

Hi Jos,

Thanks for this. But your solution deals with labels, not tick mark labels. Going back to my histogram, if you look at how the left-most bar is labelled below the x-axis, you'll see the values 1 and 2 are aligned with the very edges of the bar. But that first bar is a count of 1's (the data is all integers. So the number 1 should align with the middle of the bar.

Put another way, I want the whole of the x-axis labelling shifted half a bar width to the right, and I want to drop the 6, because there are no values of 6 in my dataset.

The other thing your solution does is affix the value as the label, not the bin from which it comes. So by implying your solution, effectively I am positioning the y-values, not the x-values, on my chart.

POSTED BY: Brad Varey

Hi Brad,

I assume shifting the mark label in hystogram is not possible. That's why I always play around with the Barchart labels for this type of plots. Sorry I can't help further.

I suggest that this question should be answered by the Wolfram Support Team.

Good luck........Jos

POSTED BY: Jos Klaps
Posted 9 years ago

Sorry, it appears my histogram got mangled in my earlier post. It appears...


POSTED BY: Brad Varey

Hi Brad, I hope this will help. See attached examples in file BarChart.nb.

data = RandomInteger[{1, 5}, 5];
 , ChartElementFunction -> "GlassRectangle"
 , LabelingFunction -> (Placed[# "%", Center] &)
 , ChartLabels -> {"1", "2", "3", "4", "5"}
 , PlotRange -> {Automatic, 6}
 , Joined -> Automatic
 , PlotLabel -> Style["Test [%]", "Title", 20]
 , BaseStyle -> {FontFamily -> "Arial", FontWeight -> "Bold", 
   FontSize -> 11}
 , ChartStyle -> 45]
POSTED BY: Jos Klaps
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract