Matlab中多区域边界绘制:相图区域分割线自动生成
Got it, let's work through this phase boundary plotting problem together! I'll show you exactly how to extract and draw those two key boundary lines from your phases array in MATLAB, just like you need.
First, let's clarify your input: your phases array is a 2D grid where each element represents the phase at that (x,y) coordinate (columns = x-axis, rows = y-axis). We need to find two types of boundaries:
- Between phase 1 and phase 2
- Between the combined phases 1/2 and phase 3
To find these boundaries, we'll check every pair of adjacent grid cells (horizontal and vertical neighbors). When two adjacent cells belong to the phase groups we care about, we'll record the midpoint between them as a boundary point.
Here's the code to do that:
% Your example phase array phases = [1 1 1 1 2 2 2 2; 1 1 1 2 2 2 2 2; 1 1 2 2 2 2 2 2; 3 3 3 3 3 3 3 3; 3 3 3 3 3 3 3 3]; % Get grid dimensions (ny = number of rows/y-values, nx = columns/x-values) [ny, nx] = size(phases); % Initialize empty arrays to store boundary points boundary_1_2 = []; % Between phase 1 and 2 boundary_12_3 = []; % Between phases 1/2 and 3 % Check horizontal adjacent cells (same row, side-by-side columns) for y = 1:ny for x = 1:nx-1 current_phase = phases(y, x); next_phase = phases(y, x+1); % Check for 1<->2 boundary if (current_phase == 1 && next_phase == 2) || (current_phase == 2 && next_phase == 1) boundary_1_2 = [boundary_1_2; x+0.5, y]; % Midpoint x = column + 0.5 end % Check for (1/2)<->3 boundary if ((current_phase == 1 || current_phase == 2) && next_phase == 3) || ... (current_phase == 3 && (next_phase == 1 || next_phase == 2)) boundary_12_3 = [boundary_12_3; x+0.5, y]; end end end % Check vertical adjacent cells (same column, stacked rows) for x = 1:nx for y = 1:ny-1 current_phase = phases(y, x); next_phase = phases(y+1, x); % Check for 1<->2 boundary if (current_phase == 1 && next_phase == 2) || (current_phase == 2 && next_phase == 1) boundary_1_2 = [boundary_1_2; x, y+0.5]; % Midpoint y = row + 0.5 end % Check for (1/2)<->3 boundary if ((current_phase == 1 || current_phase == 2) && next_phase == 3) || ... (current_phase == 3 && (next_phase == 1 || next_phase == 2)) boundary_12_3 = [boundary_12_3; x, y+0.5]; end end end
Now that we have all boundary points, we can plot them. We'll also add a heatmap of the phases as a reference (you can remove this if you don't need it):
figure; % Optional: Plot phase regions as a heatmap pcolor(phases); shading flat; colormap(viridis); % Use a nice color map (adjust as needed) hold on; % Sort boundary points to connect them smoothly (optional but recommended) boundary_1_2 = sortrows(boundary_1_2, [2 1]); % Sort by y, then x boundary_12_3 = sortrows(boundary_12_3, [2 1]); % Draw the two boundary lines plot(boundary_1_2(:,1), boundary_1_2(:,2), 'k-', 'LineWidth', 2); % Black line for 1-2 plot(boundary_12_3(:,1), boundary_12_3(:,2), 'r-', 'LineWidth', 2); % Red line for 1/2-3 % Adjust axes to match the grid structure axis([0.5 nx+0.5 0.5 ny+0.5]); set(gca, 'YDir', 'reverse'); % Makes y-axis start at the top (matches matrix indexing) % Add labels and legend xlabel('X Coordinate'); ylabel('Y Coordinate'); title('Phase Boundaries'); legend('Boundary: Phase 1 ↔ Phase 2', 'Boundary: Phases 1/2 ↔ Phase 3'); hold off;
- If you don't want the phase heatmap, just remove the
pcolorandshadinglines. - Adjust line colors, widths, or styles by modifying the
plotarguments (e.g.,'b--'for blue dashed lines). - If your phase array is larger or has more complex boundaries, this code will still work—it automatically checks all adjacent cell pairs.
This will give you exactly the two clean boundary lines you're looking for, matching the example plot you mentioned!
内容的提问来源于stack exchange,提问作者Mathias Kure

