Bar Chart Log Axis
Show older comments
I have data that I want to create a barchart from. I require the x-axis to be log10.
[counts,xb]=hist(data(:,3),nbins); %IMHIST ONLY HANDLES 8 & 16 BIT IMAGES, NOT 12BIT
bar(log10(xb),counts,'r','EdgeColor','r');
grid on;
hold on
xlim([min(log10(xb)) max(log10(xb))])
I have read that I need to do:
set(gca,'Xtick',0:4); %// adjust manually; values in log scale
set(gca,'Xticklabel',10.^get(gca,'Xtick')); %// use labels with linear values
My data is typically 4000-6000
the histogram with log10 x axis looks like the same, and the ticks are not showing?

8 Comments
dpb
on 16 Feb 2017
Just using log10(xb) doesn't change anything excepting the scaling; you've already binned the values from the linear range; hence the shape is going to be the same. Would you not want to bin log10(x) instead?
dpb
on 16 Feb 2017
Well, yes? The point is that's what you built by the hist call you made above; the binning is in linear scaling so the two are going to look the same.
If you mean that but still want to display x on a log scale, just
hBar=bar(xb,counts,'r','EdgeColor','r');
set(gca,'XScale','log')
then you can fixup range as want to make it look pretty if the autoscaling doesn't suit.
Jason
on 16 Feb 2017
Star Strider
on 16 Feb 2017
Note: Original data vector is now attached to the original Question.
dpb
on 16 Feb 2017
For what specific definition of "doesn't"? See attached that pretty-much proves it does...

The above were generated by
>> subplot(2,1,1)
>> bar(bin,cnt),set(gca,'xscale','log')
>> xlim([2000 8000])
>> title('Semilog x')
>> subplot(2,1,2)
>> bar(bin,cnt),xlim([2000 8000])
>> title('Linear x')
If you use xlim([5000 8000]) the plots are superficially the same because the range of x-values is so limited the difference in scaling between the log and linear axes isn't much at all. Widening the scale as I did show how the log axis is foreshortened in the upper half significantly, IF there's a wide-enough range for the log to make any difference.
Of course, you still have the problem that the binning was done on the linear values first both ways so the actual counts/bin number are the same.
As noted, binning log(x) instead of x will change those counts some but again with such a narrow data range it won't make much difference either way.
IF (the proverbial "big if") the data range covered a couple of decades or more, then the difference between the two would be much more obvious and using the log would have some sense...in this case there just isn't enough variation to really matter much, either way within the range itself; only if you look at the range within a much larger scale.
Jason
on 16 Feb 2017
For confirmation
>> x=(5000:8000); lx=log10(x);
>> [(lx-lx(1))./(lx(end)-lx(1));(x-x(1))./(x(end)-x(1))]
ans =
0 0.3879 0.7159 1.0000
0 0.3333 0.6667 1.0000
>>
pretty same ratios; if don't study carefully wouldn't ever tell the difference...
I moved comment to Answer; please ACCEPT to close the subject if nothing else.
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!