3 column vector countourf plot error

8 ビュー (過去 30 日間)
Gko K
Gko K 2019 年 3 月 25 日
回答済み: Riya 2025 年 4 月 22 日
I have a 3 column vector. I plot a contourf colormap but i have an error as i show below.
Here is my code:
xi=linspace(min(x),max(x),300);
yi=linspace(min(y),max(y),300);
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
figure
contourf(XI,YI,ZI,'edgecolor','none')
colormap(flipud(jet(10)))
shading interp
colorbar
title('X MOMENT','FontSize',14)
grid on
grid minor
hold on;scatter(x,y,'k','filled');
hold off
Plot shape must be as the plan in 2nd pic above. How can i fix this? I dont want to plot the area which i show in the 1st pic.
x and y, point coordinates
z, moment value.

回答 (1 件)

Riya
Riya 2025 年 4 月 22 日
Hi, 
I understand that you want to plot the figure without the triangular region specified in the first image.
For this, you can mask out that region using a custom polygon withinpolygon” function. This way, you retain full control over which parts of the contour map are visible, ensuring that it matches with your second image.
You can consider updating your code like this:
xi = linspace(min(x), max(x), 300);
yi = linspace(min(y), max(y), 300);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x, y, z, XI, YI);
% Define a polygon to exclude the top-right triangular area
% This creates a mask that only includes valid plotting region
polyX = [0 0 12 12 10]; % x-coordinates of the polygon
polyY = [0 12 12 10 12]; % y-coordinates of the polygon
% Create mask
in = inpolygon(XI, YI, polyX, polyY);
ZI(~in) = NaN; % Set values outside the polygon to NaN (will not be plotted)
% Plot the contour
figure
contourf(XI, YI, ZI, 'edgecolor', 'none')
colormap(flipud(jet(10)))
shading interp
colorbar
title('X MOMENT', 'FontSize', 14)
grid on
grid minor
hold on
scatter(x, y, 'k', 'filled');
hold off
  • You can adjust polyX and polyY according to your mesh layout.
  • The inpolygon function checks whether each point in your interpolation grid lies inside the valid domain.
  • Setting ZI(~in) = NaN ensures MATLAB ignores the invalid triangular region during contour plotting.
For more details about inpolygon, you can refer to the following documentation: 
web(fullfile(docroot, 'matlab/ref/inpolygon.html'))

カテゴリ

Help Center および File ExchangeColor and Styling についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by