Graphics from 2D data
显示 更早的评论
Hi there,
I have a matrix x of size 39505X2 and i was wondering how can i plot graphics as shown below from my data

采纳的回答
Star Strider
2023-9-26
It would be nice to have ‘x’ since I suspect that ‘x(:,1)’ cycliically repeats. Using reshape to use the cyclic repeat information to create a matrix from ‘x(:,2)’ could be the solution.
8 个评论
Weather502
2023-9-26
编辑:Torsten
2023-9-26
xy = load("x y data.mat")
xy = struct with fields:
all_data: [39505×2 double]
xy.all_data
ans = 39505×2
0.4534 0.5567
0.4534 0.6945
0.3485 0.6043
0.3591 0.6032
0.4371 0.5032
0.4418 0.5032
0.4418 0.4861
0.3958 0.4764
0.3873 0.4693
0.3873 0.4764
My data is very random and i have attached in
So you have x/y data. And where are the z-data your colour plot is based on: z = f(x,y) ?
Oh well. So much for that idea.
There are duplicated values, however they are in no regular pattern. It is likely not possible to construct anything of significance from these data. I have no idea where the matrix in the plot came from, however it was not from these data.
LD = load('x y data.mat')
LD = struct with fields:
all_data: [39505×2 double]
Data = LD.all_data
Data = 39505×2
0.4534 0.5567
0.4534 0.6945
0.3485 0.6043
0.3591 0.6032
0.4371 0.5032
0.4418 0.5032
0.4418 0.4861
0.3958 0.4764
0.3873 0.4693
0.3873 0.4764
x = Data(:,1);
y = Data(:,2);
[Ux,~,ixx] = unique(x,'stable');
Xcounts = accumarray(ixx, (1:numel(ixx)).', [], @(x){x});
% [MaxSizeX,idxx] = maxk(cellfun(@numel, Xcounts),5)
[Uy,~,ixy] = unique(y,'stable');
Ycounts = accumarray(ixy, (1:numel(ixy)).', [], @(x){x});
% [MaxSizeY,idxy] = maxk(cellfun(@numel, Ycounts),5)
figure
scatter(x, y, 5, exp(-(x-mean(x)).^2 .* (y-mean(y)).^2), '.')
colormap(turbo)
colorbar
axis('equal')

I can get this far, however I cannot figure out how to get what appears to be an imagesc plot from it.
.
I wonder if it is a density plot?
xy = load("x y data.mat")
xy = struct with fields:
all_data: [39505×2 double]
x = xy.all_data(:,1);
y = xy.all_data(:,2);
scatter(x, y)

No. density doesn't seem to match example plot.
Weather502
2023-9-26
编辑:Weather502
2023-9-26
probably i am looking some kind of density plot ot contour map to visualize my data.
Thanks
I experimented a bit more with this.
Here are some options —
LD = load('x y data.mat');
Data = LD.all_data
Data = 39505×2
0.4534 0.5567
0.4534 0.6945
0.3485 0.6043
0.3591 0.6032
0.4371 0.5032
0.4418 0.5032
0.4418 0.4861
0.3958 0.4764
0.3873 0.4693
0.3873 0.4764
x = Data(:,1);
y = Data(:,2);
[Ux,~,ixx] = unique(x,'stable');
Xcounts = accumarray(ixx, (1:numel(ixx)).', [], @(x){x});
% [MaxSizeX,idxx] = maxk(cellfun(@numel, Xcounts),5)
[Uy,~,ixy] = unique(y,'stable');
Ycounts = accumarray(ixy, (1:numel(ixy)).', [], @(x){x});
% [MaxSizeY,idxy] = maxk(cellfun(@numel, Ycounts),5)
SF = 3E+2; % Scalle Factor (For Gaussian Approximation)
z = exp(-(x-mean(x)).^2*SF) .* exp(-(y-mean(y)).^2*SF);
% figure
% plot(x, exp(-(x-mean(x)).^2*SF))
% hold on
% plot(y, exp(-(y-mean(y)).^2*SF))
% hold off
figure
scatter(x, y, 5, z, '.')
colormap(turbo)
colorbar
axis('equal')

figure
% stem3(x, y, z, '.')
hold on
scatter3(x, y, z, 2.5, z, 'o', 'filled')
hold off
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Density')
grid on
view(-37.5,30)

% axis('equal')
xv = linspace(min(x), max(x), fix(sqrt(numel(x))));
yv = linspace(min(y), max(y), fix(sqrt(numel(y))));
F = scatteredInterpolant(x, y, z);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
[X,Y] = ndgrid(xv, yv);
Z = F(X,Y);
figure
surfc(X, Y, Z, 'EdgeColor','interp')
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Density')

% [az,el] = view
[Uxy,~,ix] = unique(Data,'rows','stable');
Tally = accumarray(ix, (1:numel(ix)), [], @(x){Data(x,:)});
Density = cellfun(@(x)size(x,1), Tally);
figure
scatter3(Uxy(:,1), Uxy(:,2), Density, 5, Density, 'o', 'filled')
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Density')
title('Empirical Density Plot')

figure
hist3(Data,[250 250],'CDataMode','auto','FaceColor','interp')
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Density')
title('Empirical Density Histogram Plot')

[N,C] = hist3(Data,[250 250]);
figure
imagesc(C{1}, C{2}, N)
colormap(turbo)
colorbar
set(gca, 'YDir','normal')
xlabel('X')
ylabel('Y')
title('Image Plot Of Density Data')

The accumarray call that produced the density data for the final plot is the best I can do with an empirical approximation of the density. It counts the numbers of (x,y) pairs with the same coordinates, as determined by the last unique call. It might be possible to do a nonlinear fit of the hist3 results to a bivariate Gaussian distribution.
.
Thank you @Star Strider
Star Strider
2023-9-29
编辑:Star Strider
2023-9-29
As always, my pleasure!
EDIT — (29 Sep 2023 at 17:48)
Just out of interest —
LD = load('x y data.mat');
Data = LD.all_data;
x = Data(:,1);
y = Data(:,2);
zfcn = @(b,xy) b(1).*exp(-(xy(:,:,1)-b(2)).^2*b(3)) .* exp(-(xy(:,:,2)-b(4)).^2*b(5));
[N,C] = hist3(Data,[250 250]);
xv = linspace(min(x), max(x), size(N,1));
yv = linspace(min(y), max(y), size(N,2));
[X,Y] = ndgrid(xv, yv);
XY = cat(3, X, Y);
B = lsqcurvefit(zfcn,[50,mean(x),300,mean(y),300],XY,N)
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
B = 1×5
31.5122 0.3762 771.2200 0.5087 335.3531
ZZ = zfcn(B,XY);
figure
surf(X, Y, N, 'EdgeColor',[1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeAlpha',0.25)
hold on
surf(X,Y,ZZ, 'EdgeColor','interp')
hold off
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')

.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
