inverting Z axis in plot

조회 수: 42 (최근 30일)
Hector Colon
Hector Colon 2017년 7월 14일
댓글: Star Strider 2017년 7월 14일
Hi everyone! Im a kinda' noob at matlab and I need help inverting the Z axis on a Suface plot.
The issue I have is that I need to Invert the Z axis WITHOUT inverting the 3D figure. For Example, my Z axis goes from 1 to 0 by .25 steps and the 3D figure looks all right. I need to reverse the axis, I need it from 0 to 1 without altering the 3D figure.
I Plotted a text file as DMLread.
Here is my code:
inundation = dlmread('inundation.txt','',7,0);
nx = dlmread('inundation.txt','',[0 1 0 1]);
ny = dlmread('inundation.txt','',[1 1 1 1]);
%3D map of Run-up%
figure
flip(inundation)
mesh(inundation,'FaceColor','interp')
set(gca, 'Zdir', 'reverse')
title('3D Map of Inundation')
ylabel('Mesh width')
xlabel('Mesh length')
zlabel('Height of water')

채택된 답변

Star Strider
Star Strider 2017년 7월 14일
You only need to reverse the Z-tick labels. The plot will stay as it was.
Example
x = linspace(-1, 1, 25);
y = linspace(-1, 1, 25);
[X,Y] = meshgrid(x,y);
Z = X.^2 .* Y.^2;
figure(1)
surf(X, Y, Z)
grid on
figure(2)
surf(X, Y, Z)
grid on
zt = get(gca, 'ZTick');
set(gca, 'ZTick',zt, 'ZTickLabel',fliplr(zt))
The plots are the same, but in figure(2). the tick labels are reversed.
  댓글 수: 2
Hector Colon
Hector Colon 2017년 7월 14일
편집: Hector Colon 2017년 7월 14일
Hey Star Strider! thanks for helping me!
Although your code line works, I kind of have a little problem. If I add the lines:
zt = get(gca, 'ZTick');
set(gca, 'ZTick',zt, 'ZTickLabel',fliplr(zt))
without deactivating this one:
set(gca, 'Zdir', 'reverse')
the flip command deactivates.
so I have the situation where I need to choose the reverse command or the flipud command.
Then I tried the line:
set(gca,'YTickLabel',flipud(get(gca,'YTickLabel)))
It does invert the Axis numbers, but in the 3D figures it gives me the elevation as if the axis was in a scale from 1 to 0. I would like to read the top elevation of the 3D figure something close to 1, not 0.ish.
Any suggestions?
Star Strider
Star Strider 2017년 7월 14일
I don’t have your data or your plots, so it is difficult for me to determine what the problem is.
Do not use:
set(gca, 'Zdir', 'reverse')
with my code. That defeats the purpose, and will flip the Z-axis and the plot. Note that I do not use it in my code.
Also, don’t do this, because it has no effect on the Z-axis:
set(gca,'YTickLabel',flipud(get(gca,'YTickLabel')))
When I tried that, it just reversed the Y-ticks on my plot (which is symmetrical with respect to the X and Y axes, so I saw no other changes).
The lines I use in my code:
zt = get(gca, 'ZTick');
set(gca, 'ZTick',zt, 'ZTickLabel',fliplr(zt))
first get the Z-tick values, and second use those values to tell the routine where to put the Z-tick labels (the 'ZTick',zt part), and then what to put at those positions (the 'ZTickLabel',fliplr(zt) part) that flips the ‘zt’ row vector left-to-right.
If you want your plot to looks the way you described, just use my code. It will also work to flip the other axes if you want, so to flip the Y-axis, add these lines as well:
yt = get(gca, 'YTick');
set(gca, 'YTick',yt, 'YTickLabel',fliplr(yt))
The logic works the same way.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by