Looping of grid plot

7 次查看(过去 30 天)
Steffen B.
Steffen B. 2023-2-14
编辑: Steffen B. 2023-2-15
Hello,
with the aid of @Mathieu NOE and @Bjorn Gustavsson I was able to create a countour plot and a Nx x Ny grid partition.
The average of every "grid cell" is calculated and ploted in the first figure.
Subsequent a second plot (figure(2)) is created which consists of plain Nx x Ny grid fields.
The code is shown below:
clc,close all
clearvars
N = 100; % <= added
Lx=2;
Ly=2;
x = linspace(0,Lx,N);
y = linspace(0,Ly,N);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
figure(1)
[C,h] = contourf(X,Y,Z,10);
hold on
% let(s do a 4 x 4 area division and compute the individual cells average
Nx = 4;
Ny = 4;
dx = floor(N/Nx);
dy = floor(N/Ny);
figure(1)
for ci = 1:Nx
for cj = 1:Ny
indX = (1:dx)+(ci-1)*dx;
indY = (1:dy)+(cj-1)*dy;
% centroid of cell
xc = mean(x(indX));
yc = mean(y(indY));
% mean of submatrix
Zmean(ci,cj) = mean(Z(indX,indY),'all');
plot(xc,yc,'*r','Markersize',15);
t = text(xc+0.15,yc+0.15,num2str(Zmean(ci,cj),3),'FontSize',14,'Color','r');
end
end
M=rot90(Zmean);
dx=Lx/Nx;
dy=Ly/Ny;
% Vertices of rectangle
%column 1
R1 = [0 0;0*dx 1*dy;1*dx 1*dy;1*dx 0*dy];
R2 = [0*dx 1*dy;0*dx 2*dy;1*dx 2*dy;1*dx 1*dy];
R3 = [0*dx 2*dy;0*dx 3*dy;1*dx 3*dy;1*dx 2*dy];
R4 = [0*dx 3*dy;0*dx 4*dy;1*dx 4*dy;1*dx 3*dy];
%column 2
R5 = [1*dx 0;1*dx 1*dy;2*dx 2*dy;2*dx 0*dy];
R6 = [1*dx 1*dy;1*dx 2*dy;2*dx 2*dy;2*dx 1*dy];
R7 = [1*dx 2*dy;1*dx 3*dy;2*dx 3*dy;2*dx 2*dy];
R8 = [1*dx 3*dy;1*dx 4*dy;2*dx 4*dy;2*dx 3*dy];
%column 3
R9 = [2*dx 0;2*dx 1*dy;3*dx 2*dy;3*dx 0*dy];
R10 = [2*dx 1*dy;2*dx 2*dy;3*dx 2*dy;3*dx 1*dy];
R11 = [2*dx 2*dy;2*dx 3*dy;3*dx 3*dy;3*dx 2*dy];
R12 = [2*dx 3*dy;2*dx 4*dy;3*dx 4*dy;3*dx 3*dy];
%Column 4
R13 = [3*dx 0;3*dx 1*dy;4*dx 2*dy;4*dx 0*dy];
R14 = [3*dx 1*dy;3*dx 2*dy;4*dx 2*dy;4*dx 1*dy];
R15 = [3*dx 2*dy;3*dx 3*dy;4*dx 3*dy;4*dx 2*dy];
R16 = [3*dx 3*dy;3*dx 4*dy;4*dx 4*dy;4*dx 3*dy];
figure(2)
patch(R1(:,1),R1(:,2),'r','EdgeColor','k')
hold on
patch(R2(:,1),R2(:,2),'b','EdgeColor','k')
hold on
patch(R3(:,1),R3(:,2),'g','EdgeColor','k')
hold on
patch(R4(:,1),R4(:,2),'y','EdgeColor','k')
hold on
patch(R5(:,1),R5(:,2),'y','EdgeColor','k')
hold on
patch(R6(:,1),R6(:,2),'g','EdgeColor','k')
hold on
patch(R7(:,1),R7(:,2),'b','EdgeColor','k')
hold on
patch(R8(:,1),R8(:,2),'r','EdgeColor','k')
hold on
patch(R9(:,1),R9(:,2),'r','EdgeColor','k')
hold on
patch(R10(:,1),R10(:,2),'b','EdgeColor','k')
hold on
patch(R11(:,1),R11(:,2),'g','EdgeColor','k')
hold on
patch(R12(:,1),R12(:,2),'y','EdgeColor','k')
hold on
patch(R13(:,1),R13(:,2),'y','EdgeColor','k')
hold on
patch(R14(:,1),R14(:,2),'g','EdgeColor','k')
hold on
patch(R15(:,1),R15(:,2),'b','EdgeColor','k')
hold on
patch(R16(:,1),R16(:,2),'r','EdgeColor','k')
I would like to automate the generation of the grid (found in the code under "% Vertices of rectangle") or create it via a loop so that the individual points R1 to RN no longer have to be created manually for a large number.
Likewise, the following illustration should be automated or looped with (found in the code under "figure(2)") so that the "patch(..)" lines no longer have to be created manually for a large number.
Is it possible to assign a specific colour to each cell depending on the value of the mean value of the respective cell /for figure(2))? E.g. at <=0.125 red, 0.125 <mean<=0.5 blue, etc.
Maybe someone has a hind how to achieve this.
Best Regards
Steffen B.

采纳的回答

Voss
Voss 2023-2-14
First, I'm not sure the first plot is correct. You can turn on a colorbar to see that the text numbers don't make sense with the actual contour data, e.g., the upper-left is 1.93 but that dark blue color is less than 0 on the colorbar:
clc,close all
clearvars
N = 100; % <= added
Lx=2;
Ly=2;
x = linspace(0,Lx,N);
y = linspace(0,Ly,N);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
figure%(1)
[C,h] = contourf(X,Y,Z,10);
hold on
% let(s do a 4 x 4 area division and compute the individual cells average
Nx = 4;
Ny = 4;
dx = floor(N/Nx);
dy = floor(N/Ny);
% figure(1)
for ci = 1:Nx
for cj = 1:Ny
indX = (1:dx)+(ci-1)*dx;
indY = (1:dy)+(cj-1)*dy;
% centroid of cell
xc = mean(x(indX));
yc = mean(y(indY));
% mean of submatrix
Zmean(ci,cj) = mean(Z(indX,indY),'all');
plot(xc,yc,'*r','Markersize',15);
t = text(xc+0.15,yc+0.15,num2str(Zmean(ci,cj),3),'FontSize',14,'Color','r');
end
end
colorbar
The problem is reversing the y/row and x/column indexing of Z and Zmean. I think it's fixed below:
clc%,close all
clearvars
N = 100; % <= added
Lx=2;
Ly=2;
x = linspace(0,Lx,N);
y = linspace(0,Ly,N);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
figure%(1)
[C,h] = contourf(X,Y,Z,10);
hold on
% let(s do a 4 x 4 area division and compute the individual cells average
Nx = 4;
Ny = 4;
dx = floor(N/Nx);
dy = floor(N/Ny);
% figure(1)
Zmean = zeros(Ny,Nx); % pre-allocate
for ci = 1:Nx
indX = (1:dx)+(ci-1)*dx; % these don't depend on cj/y, so they
xc = mean(x(indX)); % can be moved out of the inner loop
for cj = 1:Ny
indY = (1:dy)+(cj-1)*dy;
% centroid of cell
yc = mean(y(indY));
% mean of submatrix
Zmean(cj,ci) = mean(Z(indY,indX),'all');
plot(xc,yc,'*r','Markersize',15);
t = text(xc+0.15,yc+0.15,num2str(Zmean(cj,ci),3),'FontSize',14,'Color','r');
end
end
colorbar
Now, for the automating of the patch construction. Here's how you can specify the vertices:
dx=Lx/Nx;
dy=Ly/Ny;
% Vertices of rectangle
[xx,yy] = meshgrid((0:Nx-1)*dx,(0:Ny-1)*dy);
xx = xx(:).'+[0;0;dx;dx;0];
yy = yy(:).'+[0;dy;dy;0;0];
And here's how you can specify the colors, based on the values of Zmean.
figure%(2)
% colors of patch faces
colors = [1 0 0; 0 0 1; 0 1 0; 1 1 0]; % define your own colors
thresholds = [-Inf 0.125 0.5 1.1 Inf]; % define your own thresholds
cdata = zeros(size(xx,2),3);
for ii = 1:numel(thresholds)-1
idx = Zmean >= thresholds(ii) & Zmean < thresholds(ii+1);
cdata(idx(:),:) = repmat(colors(ii,:),nnz(idx),1);
end
patch('XData',xx,'YData',yy,'FaceColor','flat','FaceVertexCData',cdata)
I guess that the patch colors shown in your second plot don't bear any relation to Zmean and were picked arbitrarily.
  1 个评论
Steffen B.
Steffen B. 2023-2-15
编辑:Steffen B. 2023-2-15
@Voss Thank you very much for your answer.
Yes, you are right, there was a mistake in the x and y array.
Reversing the y and x solved it.
I picked the patch colors arbitrarily, because I didn't know how to script the relation to the calcualted patch mean.
The script is working just fine and is exactly what I was striving for.
Thank you very much for your support.
Have a nice day.
Best Regards
Steffen B.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by