Smoothed 2D histogram contour

31 views (last 30 days)
Rohit Gaikwad
Rohit Gaikwad on 23 Jan 2022
Edited: Ive J on 23 Jan 2022
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
Rohit Gaikwad
Rohit Gaikwad on 23 Jan 2022
How can I create smoothed plot like this
Ive J
Ive J on 23 Jan 2022
Edited: Ive J 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

Sign in to comment.

Answers (1)

Simon Chan
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 Contour Plots 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!