问题
what I need is a histogram with X axis displayed on a log scale. However, I still want every bar in a histogram to be of the same width.
Whatever, I came up with (or upon) display bars with higher values as narrower ones (they are of the same width on ordinary scale and not on log scale).
Here is what I am doing now:
edges = 10.^(c_min:0.1:c_max);
h = histc(data, edges);
bar(edges, h); set(gca, 'Xscale', 'log');
回答1:
Since your bars should be evenly spaced, you can plot them with x-values 1,2,.. and then change the axis labels.
edges = 10.^(c_min:0.1:c_max);
h = histc(data, edges);
centers = sqrt(edges(1:end-1).*edges(2:end));
bar(h)
%# fix the x-labels, x-axis extents
xlim([0.5,length(centers)+0.5])
set(gca,'xticklabel',num2str(centers(:),'%5.2f'))
回答2:
Use patch
instead of bar
:
figure, hold on
edges = 10.^(c_min:0.1:c_max);
h = histc(data, edges);
for ii = 1:numel(edges)-1
patch(...
[edges(ii) edges(ii) edges(ii+1) edges(ii+1)], ...
[0 h(ii) h(ii) 0],...
'b');
end
Although this particular code does not do what you want, that is a problem with the definition of edges
rather than the methodology, so you'd have to fiddle a bit with the edges
(perhaps re-define them for the patch
)
来源:https://stackoverflow.com/questions/11933787/log-scale-x-axis-histogram