2D-Histogram with log y and x ticks

조회 수: 5 (최근 30일)
Alessandro Maria Laspina
Alessandro Maria Laspina 2022년 7월 20일
댓글: Steven Lord 2022년 7월 20일
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?

채택된 답변

Steven Lord
Steven Lord 2022년 7월 20일
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
Alessandro Maria Laspina
Alessandro Maria Laspina 2022년 7월 20일
How can I avoid the whiitespace?
Steven Lord
Steven Lord 2022년 7월 20일
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')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by