How to plot contour of 3rd independent variable on top of 2D plot

4 views (last 30 days)
I have some issues with contour plotting. Below is the similar data:
x= [0 0.1 0.2 0.3 0.4 0.5 0 0.1 0.2 0.3 0.4 0.5 0 0.1 0.2 0.3 0.4 0.5 0 0.1 0.2 0.3 0.4 0.5 0 0.1 0.2 0.3 0.4 0.5];
y = [0 0.01 0.02 0.022 0.024 0.025 0 0.012 0.022 0.03 0.032 0.033 0 0.02 0.03 0.035 0.037 0.038 0 0.022 0.042 0.052 0.061 0.062 0 0.03 0.04 0.07 0.91 0.98];
g = [1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5];
z = [0 5.1 6 5.8 5.2 2.4 0 5.2 6.2 5.9 5.3 2.7 0 5.4 6.4 6.1 5.4 3.0 0 5.3 6.1 5.6 4.8 2.8 0 5.0 5.9 5.2 4.3 2.1];
Plotdata = [x' y' g' z']
Plotdata =
0 0 1.0000 0
0.1000 0.0100 1.0000 5.1000
0.2000 0.0200 1.0000 6.0000
0.3000 0.0220 1.0000 5.8000
0.4000 0.0240 1.0000 5.2000
0.5000 0.0250 1.0000 2.4000
0 0 2.0000 0
0.1000 0.0120 2.0000 5.2000
0.2000 0.0220 2.0000 6.2000
0.3000 0.0300 2.0000 5.9000
0.4000 0.0320 2.0000 5.3000
0.5000 0.0330 2.0000 2.7000
0 0 3.0000 0
0.1000 0.0200 3.0000 5.4000
0.2000 0.0300 3.0000 6.4000
0.3000 0.0350 3.0000 6.1000
0.4000 0.0370 3.0000 5.4000
0.5000 0.0380 3.0000 3.0000
0 0 4.0000 0
0.1000 0.0220 4.0000 5.3000
0.2000 0.0420 4.0000 6.1000
0.3000 0.0520 4.0000 5.6000
0.4000 0.0610 4.0000 4.8000
0.5000 0.0620 4.0000 2.8000
0 0 5.0000 0
0.1000 0.0300 5.0000 5.0000
0.2000 0.0400 5.0000 5.9000
0.3000 0.0700 5.0000 5.2000
0.4000 0.9100 5.0000 4.3000
0.5000 0.9800 5.0000 2.1000
In the data, x and y would be plotted with g as group numbers:
hold on
splitapply(@(x,y)plot(x,y,'LineWidth',1.5,'marker','.','markersize',10,'MarkerEdgeColor',[1 0 1],'color',[1 0 1]),Plotdata(:,1),Plotdata(:,2),findgroups(Plotdata(:,3)))
The 2D plot created from x,y and g is shown as below:
IV_example_plot.jpg
My question is how to plot z as contour on top of this 2D plot?
The desired contour plot is shown as below. I drew the contour diagram in powerpoint (not real data).
I tried many times with contourf plot, meshgrid and griddata functions.
But I am not sure what is the best way to realize this kind plot. Can you help me with this problem?

Accepted Answer

Walter Roberson
Walter Roberson on 25 Apr 2019
N = 50;
numlevel = 25;
[X, Y] = ndgrid(linspace(0,0.5,N), linspace(0,1,N));;
Z = griddata(x, y, z, X, Y);
contourf(X, Y, Z, numlev);
hold on
splitapply(.....)
hold off
  3 Comments
Walter Roberson
Walter Roberson on 25 Apr 2019
scatter3(Plotdata(:,1), Plotdata(:,2), Plotdata(:,4))
Now look along the x axis. You will see that you have a series of points at different z levels for just slightly different x, y coordinates. Interpolation based upon x, y sees that as a very wavy surface.
Looking at that data, I do not think that Plotdata(:,4) can be said to be controlled by independent variables Plotdata(:,1) and Plotdata(:,2), so I do not think you can do interpolation.
Unfortunately, delauny() triangulation and boundary() both produce bad triangulations of your surface. I do not know if the techniques at https://www.mathworks.com/matlabcentral/answers/332619-how-can-i-generate-a-mesh-of-quadrilaterals-for-a-given-2d-surface-using-matlab#answer_261134 might be useful.
Ted Hein
Ted Hein on 25 Apr 2019
Thank you so much for the answer! That is what I want as well:)

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!