Graphics from 2D data
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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
Torsten
2023 年 9 月 26 日
So you have x/y data. And where are the z-data your colour plot is based on: z = f(x,y) ?
Star Strider
2023 年 9 月 26 日
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.
.
Walter Roberson
2023 年 9 月 26 日
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
Star Strider
2023 年 9 月 26 日
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.
.
Weather502
2023 年 9 月 29 日
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 で Annotations についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
