Smoothed 2D histogram contour
Show older comments
I want to plot smoothed 2D histogram contour with the help of X & Y data, I have attached excel file of same. I was only able to plot 2D histogram. how can I add contour ?
x = importdata('x.txt');
>> y = importdata('y.txt');
>> data=[x,y];
>> hist3(data,cdatamode,'auto')

I need this type of smoothed plot
3 Comments
data = readtable('https://se.mathworks.com/matlabcentral/answers/uploaded_files/870630/xy.xlsx');
tiledlayout(1, 2)
nexttile
histogram2(data.X, data.Y, 'DisplayStyle', 'tile')
nexttile
histogram2(data.X, data.Y, 'BinMethod', 'fd')
Rohit Gaikwad
on 23 Jan 2022
data = readtable('https://se.mathworks.com/matlabcentral/answers/uploaded_files/870630/xy.xlsx');
histogram2(data.X, data.Y, 'DisplayStyle', 'tile', 'NumBins', 250)
shading interp
grid off
Another example:
h = histogram2(randn(1e6, 1), randn(1e6, 1), 'DisplayStyle', 'tile', 'NumBins', 200);
shading interp
Answers (1)
Simon Chan
on 23 Jan 2022
Try this:
data = readmatrix('xy.xlsx');
nbins=[200 200]; % You may adjust number of bins for both direction
[N,Xedges,Yedges] = histcounts2(data(:,1),data(:,2),nbins);
[X,Y] = meshgrid(Xedges(1:end-1),Yedges(1:end-1));
N = N';
scatter(X(:),Y(:),[],N(:),'filled'); % Use scatter plot
colorbar; % Colorbar with custom colormap

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


