How can I plot 3D contour plot for (x, y, z) Cartesian grid with values at each cell?

41 views (last 30 days)
What code should I use to plot a 3D contour plot? I saw there are some contour3 functions, but I don't know how to create the 2*2 matrix for my data. I have data include (x,y,z) positioning data and value at each position. I attached part of my data looks like, not all y,z values are zero.
I also attached a plot that shows closely to what I want, but I also want scales shown on the figure.
  2 Comments
Ewen Chan
Ewen Chan on 28 Jun 2017
I have a very similar problem where I have x, y, z, w data and I want to create a contour plot with that (please see attached text file).
I tested out Sriram's answer below, but it only gives me a contour plot of a slice of data, but not necessarily the whole thing, so I am wondering if someone can please help me plot my entire data set.
Thank you.
Denis Perotto
Denis Perotto on 15 Mar 2019
I have a similar problem. Seems like that is not possible at all. All I managed to do is to cycle a 'fill3' function to build a contour from several rectangles, but it cost a lot of RAM and lags.

Sign in to comment.

Answers (6)

Sriram Narayanan
Sriram Narayanan on 3 Jun 2015
Since you have values at points (x,y,z) in space, you could use the 3-D interpolation function "interp3" to interpolate between the points from the sample data and then use the "surf" function to generate the 3-D plot and the colorbar command to add the scale.
The following page talks about how to use this function.
Example code is:
generatedvalues = @(X,Y,Z)(X.^2 + Y.^3 + Z.^4);
[X,Y,Z] = meshgrid((-5:.25:5));
V = generatedvalues(X,Y,Z);
x = X(:,3,:);
y = Y(:,3,:);
z = V(:,3,:);
v = V(:,3,:);
z = Z(:,3,:);
x = squeeze(x);
y = squeeze(y);
z = squeeze(z);
v = squeeze(v);
surf(x,y,z);
colorbar
  2 Comments
Jia
Jia on 4 Jun 2015
Thank you so much for your help. Now, I know how to define the grid, but how can I color these grid cells with my list of "V" values?
Luca Greggio
Luca Greggio on 1 Apr 2020
maybe i have got the answer
close all
clear all
file = fopen('valori.dat');
imax = fscanf(file, '%d \n', 1);
x = fscanf(file, '%f \n', imax);
y = fscanf(file, '%f \n', imax);
z = fscanf(file, '%f \n', imax);
T = fscanf(file, '%f \n', imax^3);
T = reshape(T,[imax, imax, imax]);
[X,Y,Z]=meshgrid(x,y,z);
xs = squeeze(X(:,7,:));
ys = squeeze(Y(:,7,:));
zs = squeeze(Z(:,7,:));
surf(xs,ys,zs,squeeze(T(:,:,7)));
colorbar
this get data from the file 'valori.txt' and when i apply the procedure in the answer, i simply add the 4th value in surf which is C, standing for color, this give the opportunity to give a color to the grid from your V value, in my case is T, i am plotting a 3D Heat equation soulution, i hope this can help

Sign in to comment.


John A
John A on 22 Nov 2017
I have 5 surfaces, each is a 5 by 10 matrix. Each entry represents force value and is 1inch from neighboring entries on the surface. I was hoping to stack these surfaces to create a 3D block and contour (or color code) the block based on the magnitudes of these value. The expected outcome will be a block similar to Jia's block model shown above. Can someone please direct me on how to do this in matlab? I am a beginner in matlab but can find my way out if pointed in the right direction.
Thank you

Kabilan K
Kabilan K on 9 Nov 2019
Anyone have got the required answers for the question of John A or Jia

Denis Perotto
Denis Perotto on 10 Nov 2019
Download free Paraview (visualization tool) from https://www.paraview.org/download/. It will cost you some time to deal with VTK files, but anyway, MATLAB is not a tool for 3D contour visualization.

Mingta Yang
Mingta Yang on 5 Mar 2021
Did you try fill3. It might be what you need.

Pavl M.
Pavl M. on 7 Nov 2024 at 13:32
Edited: Pavl M. on 7 Nov 2024 at 14:15
You need to create suitable grid on your values loaded.
Please accept my solution:
(Works in NCE MPPL TCE Octave and Matlab):
clc
clear all
close all
% load your- x,y,z,v from file
%[x,y,z] = meshgrid(your_x,your_y,your_z);
%v = ndgrid(your_v)
%create detailed grid with next function
%Hattrell(2024). qgriddata (https://www.mathworks.com/matlabcentral/fileexchange/10399-qgriddata), MATLAB Central File Exchange. Extrait(e) le novembre 7, 2024.
GrdiSize = 1000;
dx = (yourxmax-yourxmin)/GridSize;
dy = (yourymax-yourymin)/GridSize;
dz = (yourzmax-yourzmin)/GridSize;
[x,y,z,v] = qgriddata(yourxmin,yourxmax,dx,yourymin,yourymax,dy,yourzmin,yourzmax,dz,yourx,youry,yourz,yourv);
step = dz;
zslice = yourzmin:step:yourzmax;
figure
h = slice(x,y,z,v,[],[],zslice,'spline');
colormap(turbo)
colorbar
set(h,'EdgeColor','none')
I run it in Octave on formula(equation) defined custom (works for any arbitrary function) and produced both slices and full 3D plot of field (function) values within a specified rectangular(cubic) region:
Should you need the exact running script code matter, more details for specific solutions service, contact me more in private.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!