# Plot contour3 of data consist x, y, phi

6 views (last 30 days)
foroogh behroozi on 15 Mar 2024
Answered: Namnendra on 24 Jul 2024 at 19:36
Hello,
I need guidance related to contour3 ploting of my data. I have a matrix contains as below data:
x y phi s
1 1 1 0
1 1 2 0
1 1 3 0
1 2 1 0
1 2 2 1
1 2 3 1
2 1 1 0
2 1 2 1
.....
which x varies from 1 to 4, y varies from 1 to 2, phi varies from 1 to 3 and s could have just 0 and 1 (my matrix is 689689 by 4, and this is just a sample of my data). I need to contour3 of my data for the rows which just s=1. So for first step I have to filter my data. The rest of my data which has x, y and phi in each row represents a point. I need to contour3 plot for each level of phi which contains lots of point in that level of phi. I tried to plot it but it create a contour3 for each point in each level (For example, I have more than 1000 points which has same phi so in each level of contour3 for a phi it creates 1000 contour3 around each point!). But I need a contour3 for more than 1000 point on each level. It would be great if you could guide me. Thanks.

Namnendra on 24 Jul 2024 at 19:36
Hi Foroogh,
I understand that you want to create a `contour3` plot for your data. To do this, you need to follow these steps:
1. Filter the Data: Extract the rows where `s = 1`.
2. Separate Data by `phi` Levels: For each unique value of `phi`, extract the corresponding `x`, `y`, and `s` values.
3. Create a Grid for Contouring: Use `x` and `y` to create a grid, and interpolate `s` values onto this grid.
4. Plot the Contours: Use the `contour3` function to plot the contours for each `phi` level.
Here is a step-by-step guide using MATLAB code:
Step 1: Filter the Data
Assuming your data is stored in a matrix `data`:
% Sample data matrix
data = [
1 1 1 0;
1 1 2 0;
1 1 3 0;
1 2 1 0;
1 2 2 1;
1 2 3 1;
2 1 1 0;
2 1 2 1;
% ... (more data)
];
% Filter rows where s == 1
filteredData = data(data(:, 4) == 1, :);
Step 2: Separate Data by `phi` Levels
% Unique phi levels
phiLevels = unique(filteredData(:, 3));
% Initialize cell array to store data for each phi level
phiData = cell(length(phiLevels), 1);
for i = 1:length(phiLevels)
phi = phiLevels(i);
phiData{i} = filteredData(filteredData(:, 3) == phi, :);
end
Step 3: Create a Grid for Contouring
For each `phi` level, create a grid and interpolate `s` values:
% Define the grid resolution
gridResolution = 0.1; % Adjust as needed
[Xq, Yq] = meshgrid(min(data(:, 1)):gridResolution:max(data(:, 1)), ...
min(data(:, 2)):gridResolution:max(data(:, 2)));
% Initialize cell array to store interpolated grids
interpolatedGrids = cell(length(phiLevels), 1);
for i = 1:length(phiLevels)
phiPoints = phiData{i};
X = phiPoints(:, 1);
Y = phiPoints(:, 2);
S = phiPoints(:, 4);
% Interpolate s values onto the grid
Sq = griddata(X, Y, S, Xq, Yq, 'natural');
% Store the interpolated grid
interpolatedGrids{i} = Sq;
end
Step 4: Plot the Contours
Use `contour3` to plot the contours for each `phi` level:
figure;
hold on;
for i = 1:length(phiLevels)
phi = phiLevels(i);
Sq = interpolatedGrids{i};
% Plot contour3 for the current phi level
contour3(Xq, Yq, Sq, 'DisplayName', ['phi = ', num2str(phi)]);
end
hold off;
xlabel('X');
ylabel('Y');
zlabel('S');
legend('show');
title('Contour3 Plot for Different Phi Levels');
Explanation
1. Filtering: The data is filtered to include only rows where `s = 1`.
2. Separation by `phi` Levels: The filtered data is separated into different groups based on the unique values of `phi`.
3. Grid Creation and Interpolation: For each `phi` level, a grid is created using `meshgrid`, and the `griddata` function is used to interpolate `s` values onto this grid.
4. Plotting: The `contour3` function is used to plot the contours for each `phi` level. The `DisplayName` property is used to label each contour plot with its corresponding `phi` value.
This approach ensures that you get a single contour plot for each `phi` level, rather than individual contours for each data point. Adjust the `gridResolution` as needed to get the desired level of detail in your contour plots.
I hope the above information helps you.
Thank you.

### Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!