Help with 3D contours aligned to discrete surface data

Running the included code produces the included plot.
Basically a 3D plot of Z along a non-equi-spaced orthogonal mesh in x and y. It generates 3D contours, but they do not lie on the surface. Most are hidden on the reverse side.
Close inspection suggests they appear to lie on a 'smoothed' invisible surface offset from coloured surface. I'm obviously missing something, but if possible, i wish to have black contour lines aligned with the colour interpolated surface.
Data;
figure()
surf(pc,of,c,...
'LineStyle','-',...
'EdgeColor',[0.5 0.5 0.5],...
'FaceColor','interp')
zmin=min(c(:));
zmax=max(c(:));
xlabel 'P_c [bar]'
ylabel '(o/f)_m'
zlabel '{\rho} [kg/m^3]'
set(gca,'xscale','log','yscale','log')
hold on
number=20;
range=zmax-zmin;
inct=range/number;
contourl=zmin:inct:zmax;
contour3(pc,of,c,...
contourl,...
'k')
hold off
colormap (jet(256))
colorbar

 Accepted Answer

Reading the files is not an option, so I went straight for the ‘Problem.fig’ file.
I’m not certain that the contour lines aren’t plotting on the surface, however they appear to be the same colours as the surface, and that may be hiding them. One option is to make the surface slightly more transparent, and perhaps also to make the lines black (however the line colour does not appear to change) —
F = openfig(websave('Problem','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1152063/Problem.fig'));
getF = get(F);
Ax = findobj(F, 'Type','Axes');
getAx = get(Ax);
Kids = Ax.Children
Kids =
2×1 graphics array: Contour Surface
Kids(2).FaceAlpha = 0.75; % Necessary (Experiment With Different Values)
hc = Kids(1);
hc.LineWidth = 2; % Optional
hc.LineColor = 'k'; % Optional
I tried a few other experiments, however this is the only one that appears to work reliably.
.

8 Comments

Had a play with your suggestions. My contour lines look grey from the 'front' where they appear behind the surface, and black where they appear to break through. Tried to include a side-view to show the discrepancy.
I could post a listing of code and data if it helps?
That, and any necessary data would definitely help.
I experimented with a few other options, such as slightly adjusting ‘hc.XData’ and ‘hcYData’ to position them in front of the surface.
However the solution could be to increase the resolution of the surface matrices, since the contours may be positioned more accurately than the surface is calculated, so the surface is ‘hiding’ them.
pc=[1,2,5,10,20,30,70];
of=[.4,.5,.7,1,2,5,7,10,15,20,30,50,100];
c = ...
[0.0017674827252359125 0.0034928953583669106 0.0086195366120220048 0.017124176611165637 ...
0.034143398442433019 0.051218736178458645 0.12001289207216918;
0.0018068005529565489 0.003551383099562115 0.0086803360476427321 0.017076112299181902 ...
0.033618638796160928 0.049991490131899421 0.11475388176066162;
0.0019531349738764457 0.0039062699477528228 0.0097656748693817144 0.019531349738762884 ...
0.039062699477527184 0.058594049216288857 0.1333262758374037;
0.0017431832567206787 0.0034863666754282947 0.0087159170479174635 0.017431834458060094 ...
0.034863669428388061 0.052295504483000917 0.122022845014209;
0.0014131809499508014 0.0028267829949186253 0.00706791058386975 0.01413679607679336 ...
0.02827498281119966 0.042413403094375669 0.098967951293817455;
0.0011158017855461457 0.0022453178307318862 0.0056602634883156549 0.011389014741872509 ...
0.022904549726755753 0.03445853191114668 0.080840987037328324;
0.0011743831519246939 0.0023284213425193477 0.0057638931189663706 0.011459136917774138 ...
0.022816253547166786 0.0341614268453073 0.079555389365864385;
0.0014452051461961775 0.0028677243059421929 0.0071059195099525277 0.014134868528229745 ...
0.028149902485428785 0.042143489995637114 0.098064998893735253;
0.0019305793044475264 0.0038654804342955224 0.0096915251056551334 0.019439727316775662 ...
0.039007713832494059 0.058629460927678553 0.13738134183824333;
0.0025020721096050808 0.0050299267323266651 0.012654857267640634 0.02542067292452125 ...
0.0510435257542246 0.0767284149515598 0.17974873795853771;
0.0037672597528130487 0.0075526417223835659 0.018932356940629613 0.037929126577915619 ...
0.0759682952336209 0.11403727534491258 0.26644307816087959;
0.0062373784033877607 0.012477153937999945 0.031199269423554773 0.0624063106059884 ...
0.12482552364311707 0.18724804181050336 0.43695232360844904;
0.012153601056849095 0.02430720993001835 0.060768045848069011 0.12153610513709463 ...
0.24307227729500588 0.36460844797376918 0.85075317616518231];
figure()
surf(pc,of,c,...
'LineStyle','-',...
'EdgeColor',[0.5 0.5 0.5],...
'FaceColor','interp',...
'FaceAlpha', 0.75)
zmin=min(c(:));
zmax=max(c(:));
xlabel 'P_c [bar]'
ylabel '(o/f)_m'
zlabel '{\rho} [kg/m^3]'
set(gca,'xscale','log','yscale','log')
hold on
number=20;
range=zmax-zmin;
inct=range/number;
contourl=zmin:inct:zmax;
contour3(pc,of,c,...
contourl,...
'k')
hold off
colormap (jet(256))
colorbar
The data input is from an external computer program.
When you say increase the resolution of the surface matrices, i can make the program increase the sample intervals and range of [pc] and [of] vectors. [c] will increase in accordance with these. I would prefer to maintain liner interpolation between data points without cubic type smoothing.
Might meshgrid be something that might help in this respect?
A finer mesh might be the best approach, although I cannot understand the problems contour3 is having with it. I haven’t encountered this sort of problem with it before. (I couldn’t find an appropriate way to interpolate it because that would likely involve something other than linear interpolation — most likely 'pchip' — since a linear interpolation would not solve the problem. That would only create more linearly-interpolated areas in the surface, and the contour lines would still be behind them.)
The only thing I can think of is that contour3 is interpolating and the contours are ‘behind’ the surface for that reason. They do not exactly match it.
The only interim solution I can suggest is something like this —
contour3(pc*0.95,of*0.95,c,20,'k')
That ‘pulls’ the contours slightly to the front of the surface. It ‘sort of’ solves the problem, however there are compromises. (I had experimented with this approach with the figure file. It’s easier with the code.)
I implemented it here —
pc=[1,2,5,10,20,30,70];
of=[.4,.5,.7,1,2,5,7,10,15,20,30,50,100];
c = ...
[0.0017674827252359125 0.0034928953583669106 0.0086195366120220048 0.017124176611165637 ...
0.034143398442433019 0.051218736178458645 0.12001289207216918;
0.0018068005529565489 0.003551383099562115 0.0086803360476427321 0.017076112299181902 ...
0.033618638796160928 0.049991490131899421 0.11475388176066162;
0.0019531349738764457 0.0039062699477528228 0.0097656748693817144 0.019531349738762884 ...
0.039062699477527184 0.058594049216288857 0.1333262758374037;
0.0017431832567206787 0.0034863666754282947 0.0087159170479174635 0.017431834458060094 ...
0.034863669428388061 0.052295504483000917 0.122022845014209;
0.0014131809499508014 0.0028267829949186253 0.00706791058386975 0.01413679607679336 ...
0.02827498281119966 0.042413403094375669 0.098967951293817455;
0.0011158017855461457 0.0022453178307318862 0.0056602634883156549 0.011389014741872509 ...
0.022904549726755753 0.03445853191114668 0.080840987037328324;
0.0011743831519246939 0.0023284213425193477 0.0057638931189663706 0.011459136917774138 ...
0.022816253547166786 0.0341614268453073 0.079555389365864385;
0.0014452051461961775 0.0028677243059421929 0.0071059195099525277 0.014134868528229745 ...
0.028149902485428785 0.042143489995637114 0.098064998893735253;
0.0019305793044475264 0.0038654804342955224 0.0096915251056551334 0.019439727316775662 ...
0.039007713832494059 0.058629460927678553 0.13738134183824333;
0.0025020721096050808 0.0050299267323266651 0.012654857267640634 0.02542067292452125 ...
0.0510435257542246 0.0767284149515598 0.17974873795853771;
0.0037672597528130487 0.0075526417223835659 0.018932356940629613 0.037929126577915619 ...
0.0759682952336209 0.11403727534491258 0.26644307816087959;
0.0062373784033877607 0.012477153937999945 0.031199269423554773 0.0624063106059884 ...
0.12482552364311707 0.18724804181050336 0.43695232360844904;
0.012153601056849095 0.02430720993001835 0.060768045848069011 0.12153610513709463 ...
0.24307227729500588 0.36460844797376918 0.85075317616518231];
figure()
surf(pc,of,c,...
'LineStyle','-',...
'EdgeColor',[0.5 0.5 0.5],...
'FaceColor','interp',...
'FaceAlpha', 0.75)
zmin=min(c(:));
zmax=max(c(:));
xlabel 'P_c [bar]'
ylabel '(o/f)_m'
zlabel '{\rho} [kg/m^3]'
set(gca,'xscale','log','yscale','log')
hold on
number=20;
range=zmax-zmin;
inct=range/number;
contourl=zmin:inct:zmax;
contour3(pc*0.95,of*0.95,c,20,'k')
hold off
colormap (jet(256))
Since you specify 20 contours, I decided to let contour3 calculate those levels itself. I’m not certain that it made much of a difference, though.
.
I can live with that.
Thanks very much for your help on this.
Much appreciated.
As always, my pleasure!
I wish there was a better (at least a more robust) solution.

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Products

Tags

Community Treasure Hunt

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

Start Hunting!