2D-Histogram with log y and x ticks

10 views (last 30 days)
Hello,
i am trying to use the solution in Plot 2D-histogram for X and Y - (mathworks.com), and add
set(gca,'Yscale','log')
set(gca,'Xscale','log')
The issue is that this makes the grids unequally spaced. I want the grids to have the same spacing. Also, I want to apply the following bin edges:
edges=[0 1:1:6 1e1 1e2 1e3 1e4 1e7];
The solution that I have now is the following:
edges=[0 1:1:6 1e1 1e2 1e3 1e4 1e7];
data = rand(100,2);
hh3 = hist3(data, 'Nbins',[1 1]*60);
image(flipud(hh3))
ax = gca;
xt = ax.XTick;
yt = ax.YTick;
ax.XTickLabel = xt*10;
set(ax, 'YTick',[0 yt], 'YTickLabel', [flip([0 yt])]*10)
colorbar
set(gca,'Yscale','log')
set(gca,'Xscale','log')
This also creates the issue of whitespace outside the range of values available. Additionally, I would also like to use a discrete color bar with discrete color values.
Any suggestions?

Accepted Answer

Steven Lord
Steven Lord on 20 Jul 2022
Instead of using hist3 I recommend you use histogram2.
edges=2.^(-8:0) % Set the lower limit to be whatever will cover your data
edges = 1×9
0.0039 0.0078 0.0156 0.0312 0.0625 0.1250 0.2500 0.5000 1.0000
data = rand(100,2);
histogram2(data(:, 1), data(:, 2), ... % X and Y data
edges, edges, ... % X and Y edges
'DisplayStyle', 'tile')
set(gca, 'XScale', 'log', 'YScale', 'log')
  2 Comments
Steven Lord
Steven Lord on 20 Jul 2022
If you want the bins that have no data to be displayed, specify ShowEmptyBins. The default is false, meaning not to show those empty bins.
edges=2.^(-8:0); % Set the lower limit to be whatever will cover your data
data = rand(100,2);
histogram2(data(:, 1), data(:, 2), ... % X and Y data
edges, edges, ... % X and Y edges
'DisplayStyle', 'tile', ...
'ShowEmptyBins', true)
set(gca, 'XScale', 'log', 'YScale', 'log')
colorbar
With only 100 data points in this sample data set, many of the bins are empty. If you used a million points instead:
figure
data = rand(1e6,2);
histogram2(data(:, 1), data(:, 2), ... % X and Y data
edges, edges, ... % X and Y edges
'DisplayStyle', 'tile', ...
'ShowEmptyBins', true)
set(gca, 'XScale', 'log', 'YScale', 'log')
colorbar
Or displayed the histogram2 using its default DisplayStyle, you can see that the bins associated with smaller X and/or Y values really don't contain much data.
figure
histogram2(data(:, 1), data(:, 2), ... % X and Y data
edges, edges, ... % X and Y edges
'ShowEmptyBins', true)
set(gca, 'XScale', 'log', 'YScale', 'log')

Sign in to comment.

More Answers (0)

Categories

Find more on Colormaps 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!