Filled contour plot on other planes (X-Z or Y-Z)
15 views (last 30 days)
Show older comments
Volodymyr Rodin
on 10 Feb 2023
Edited: Benjamin Kraus
on 15 Feb 2023
Hello I was looking for a nice way to visualize my 3D data.
Firstly, I did it using slices of pcolor plots in different place.
However, it's hard to interpret values like this
So I was trying to repeat this plot with contourf but it only allows plotting in X-Y plane.
Is any possibility to create filled contours in other planes?
I found only the contourslice function for contours.
0 Comments
Accepted Answer
Benjamin Kraus
on 13 Feb 2023
contourf works fine when used with an hgtransform, except it is not updating the automatically selected limits correctly, so you will need to update the limits to make sure your rotated contour object is visible in the axes.
I'm not quite sure why you are getting the error "The new value for the Matrix property may cause rendering problems.", but I suspect it has nothing to do with contour, but rather the specific Matrix you are using is not well defined. If you can provide the specific matrix that you used that produced that error message, that would be helpful to get a better idea of why you get that error message.
Considering the example code you provided one of your comments, with some small modifications:
% Default Matrix (eye(4)) works fine, as expected:
fig = figure;
ax = axes('Parent',fig);
t = hgtransform('Parent',ax);
Z = peaks;
contourf(Z,32,':','Parent',t);
% Rotating 90 degrees around X moves the contour into the X/Z plane, and
% requires updating the limits and view.
fig = figure;
ax = axes('Parent',fig);
t = hgtransform('Parent',ax);
Z = peaks;
contourf(Z,32,':','Parent',t);
t.Matrix = makehgtform('xrotate',pi/2);
ylim([-1 1])
zlim([1 49]);
view(0,0)
% The contour starts out at z = 0, x = [1 49], and y = [1 49]. If you
% rotate 45 degrees around the X-axis, the new location is:
% x = [1 49]
% y = sqrt(2)/2.*[1 49]
% z = sqrt(2)/2.*[1 49]
beforeX = [1 49];
beforeY = [1 49];
beforeZ = [0 0];
beforeLim = [beforeX; beforeY; beforeZ; [0 0]];
transformationMatrix = makehgtform('xrotate',pi/4);
afterLim = transformationMatrix*beforeLim;
afterX = afterLim(1,:);
afterY = afterLim(2,:);
afterZ = afterLim(3,:);
fig = figure;
ax = axes('Parent',fig);
t = hgtransform('Parent',ax);
Z = peaks;
contourf(Z,32,':','Parent',t);
t.Matrix = transformationMatrix;
xlim(afterX)
ylim(afterY)
zlim(afterZ)
% The contour starts out at z = 0, x = [1 49], and y = [1 49]. If you
% rotate 2 degrees (pi/90) around the Y-axis ([0 1 0]), the new location is:
% x = ~[1 49]
% y = ~[1 49]
% z = ~[-1.7 -0.03]
beforeX = [1 49];
beforeY = [1 49];
beforeZ = [0 0];
beforeLim = [beforeX; beforeY; beforeZ; [0 0]];
transformationMatrix = makehgtform('axisrotate',[0 1 0],pi/90);
afterLim = transformationMatrix*beforeLim;
afterX = sort(afterLim(1,:));
afterY = sort(afterLim(2,:));
afterZ = sort(afterLim(3,:));
fig = figure;
ax = axes('Parent',fig);
t = hgtransform('Parent',ax);
Z = peaks;
contourf(Z,32,':','Parent',t);
t.Matrix = transformationMatrix;
xlim(afterX)
ylim(afterY)
zlim(afterZ)
% Note that makehgtform('axisrotate',[0 1 0],pi/90) is equivalent to
% makehgtform('yrotate',pi/90), so you will get the same answer.
mat1 = makehgtform('axisrotate',[0 1 0],pi/90)
mat2 = makehgtform('yrotate',pi/90)
isequal(mat1, mat2)
13 Comments
Benjamin Kraus
on 15 Feb 2023
Edited: Benjamin Kraus
on 15 Feb 2023
Thanks. This does seem to be a bug. I've reported the bug to the developers. In the meantime, if setting FaceAlpha to 0.99 works, I would continue doing that.
If I had to guess, when you set FaceAlpha to anything close to 1, there is an optimization that is being done for performance reasons that skips some transparency calculations, and the bug is in that pathway. When you set the FaceAlpha to <0.99, it will likely take longer to render, because it is doing extra transparency calculations, but it results in a more accurate picture.
More Answers (1)
Walter Roberson
on 10 Feb 2023
Create a hgtransform() and parent it to the axes. Extract a 2d slice of the volume (interpn() or even just array access and squeeze()) and contoutf() that, parenting it to the hgtransform (pass in the transform handle where you would pass in an axes). Now set the Matrix property of the transform to rotate it to the desired plane. makehgtform is very useful for creating the matrix.
Note that the rightmost transform in makehgtform is one executed first.
6 Comments
See Also
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!