I want to create a contour from this matrix with x,y and z data. x is the length, y is the width and z is the deviatoric strain.
I want to create a contour plot instead. Is that possible?
figure; hold on ; box on
scatter(x,y,[20],-z,'filled')
ylabel('Width, \ $(\mathrm{m})$','FontSize',15); xlabel('Length, \ $(\mathrm{m})$','FontSize',15,'FontName','times')

 채택된 답변

Voss
Voss 2024년 4월 23일

0 개 추천

M = readmatrix('k.txt');
x = M(:,1);
y = M(:,2);
z = M(:,3);
I = scatteredInterpolant(x,y,z);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
N = 80;
X = linspace(min(x),max(x),N);
Y = linspace(min(y),max(y),N);
[X,Y] = meshgrid(X,Y);
Z = I(X,Y);
contour(X,Y,Z)
colorbar

댓글 수: 6

mads skibsted
mads skibsted 2024년 4월 23일
Hi, Thanks,
But it is really import to keep the hole in the middle. There is no contours at that area..
Voss
Voss 2024년 4월 23일
You're welcome!
M = readmatrix('k.txt');
x = M(:,1);
y = M(:,2);
z = M(:,3);
I = scatteredInterpolant(x,y,z);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
N = 80;
X = linspace(min(x),max(x),N);
Y = linspace(min(y),max(y),N);
[X,Y] = meshgrid(X,Y);
idx = X.^2+Y.^2 <= 4;
X(idx) = NaN;
Y(idx) = NaN;
Z = I(X,Y);
contour(X,Y,Z)
colorbar
some minor improvements (??) like the log spaced v (levels) + automatic minimum radius computation for the inner hole
M = readmatrix('k.txt');
x = M(:,1);
y = M(:,2);
z = M(:,3);
%% uncomment the code below if you want to overlay the scatter and the ontour plot
% figure;
% hold on ;
% box on
% scatter(x,y,[20],z,'filled')
% colormap('jet');
% colorbar('vert');
% ylabel('Width, \ $(\mathrm{m})$','FontSize',15);
% xlabel('Length, \ $(\mathrm{m})$','FontSize',15,'FontName','times')
% axis square
%%%%%%
% v = linspace(min(z),max(z),50); % define the levels for the contour plot
v = logspace(log10(min(z)),log10(max(z)),50);
I = scatteredInterpolant(x,y,z,'linear','none');
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
N = 100;
X = linspace(min(x),max(x),N);
Y = linspace(min(y),max(y),N);
[X,Y] = meshgrid(X,Y);
[th,r] = cart2pol(x,y);
idx = X.^2+Y.^2 <= (ceil(min(r)))^2;
X(idx) = NaN;
Y(idx) = NaN;
Z = I(X,Y);
[c,h] = contour(X,Y,Z,v);
colormap('jet');
grid on
axis square
% clabel(c,h,v,'LabelSpacing',3000,'Color','b','FontWeight','bold')
colorbar('vert')
Voss
Voss 2024년 4월 24일
Those are definitely improvements. Thank you!
Mathieu NOE
Mathieu NOE 2024년 4월 24일
hello @Voss
tx for the nice comment :)

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

추가 답변 (2개)

Mathieu NOE
Mathieu NOE 2024년 4월 23일

0 개 추천

hello
sure you can do that
notice that I needed to create lot of levels to display the outer contour line because your z data is highly concentrated in the lower portion of the range (we can see that by plotting a histogram)
data = readmatrix('k.txt');
x = data(:,1);
y = data(:,2);
z = data(:,3);
histogram(z,200)
% Interpolate the scattered data
xd = linspace(min(x),max(x),100);
yd = linspace(min(y),max(y),100);
v = linspace(min(z),max(z),200); % define the levels for the contour plot
[xq,yq] = meshgrid(xd,yd);
zq = griddata(x,y,z,xq,yq);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
% replace NaN's generated by griddate with 0 (or a value lower than min(z)
zq(isnan(zq)) = 0;
% contour plot
[c,h] = contour(xq,yq,zq,v);
grid on
axis square

댓글 수: 2

this could be an alternative , with less contour lines
now the levels are generated with a log scale
and I use clabel to show what are the corresponding z value
data = readmatrix('k.txt');
x = data(:,1);
y = data(:,2);
z = data(:,3);
% Interpolate the scattered data
xd = linspace(min(x),max(x),100);
yd = linspace(min(y),max(y),100);
% v = linspace(min(z),max(z),200); % define the levels for the contour plot
v = logspace(log10(min(z)),log10(max(z)),10);
[xq,yq] = meshgrid(xd,yd);
zq = griddata(x,y,z,xq,yq);
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
% replace NaN's generated by griddate with 0 (or a value lower than min(z)
zq(isnan(zq)) = 0;
% contour plot
[c,h] = contour(xq,yq,zq,v);
grid on
axis square
clabel(c,h,v,'LabelSpacing',3000,'Color','b','FontWeight','bold')
colorbar('vert')
mads skibsted
mads skibsted 2024년 4월 23일
Hi, Thanks,
But it is reallt import to keep the hole in the middle. There is no contours at that area..

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

Walter Roberson
Walter Roberson 2024년 4월 23일

0 개 추천

댓글 수: 3

mads skibsted
mads skibsted 2024년 4월 24일
Hi walter,
I have looked into this funtion, but could not make it work..
Would you maybe try with the data I have?
It works on my side but with tricontour I don't know how you can manage to keep the inner hole
data = readmatrix('k.txt');
x = data(:,1);
y = data(:,2);
z = data(:,3);
% v = linspace(min(z),max(z),20); % define the levels for the contour plot
v = logspace(log10(min(z)),log10(max(z)),50);
tri=delaunay(x,y); % triangulate scattered data
[C,h]=tricontour(tri,x,y,z,v);
% clabel(C,h)
colormap('jet');
grid on
axis square
% clabel(c,h,v,'LabelSpacing',3000,'Color','b','FontWeight','bold')
colorbar('vert')
Mathieu NOE
Mathieu NOE 2024년 4월 24일
so you probably should stick with the solution based on scatteredInterpolant

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

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

질문:

2024년 4월 23일

댓글:

2024년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by