How to add vertical line in z direction in YZ view meshplot?

조회 수: 4 (최근 30일)
Dror Hershkovitz
Dror Hershkovitz 2023년 12월 10일
댓글: Dyuman Joshi 2023년 12월 10일
I have a 2D matrix that I plot in meshplot in XY and YZ view. For the YZ view I want to overlay a vertical line centered on the 0 of the Y axis.I need something similar to yline(0) that will be viewable in YZ view of a mesh plot.
Right now I "solved" it using annotation by adding a line annotation, but this solution is not good enough for me, cause sometime I have a figure with several subplots, and since the annotation position are in relative to the figure, and not the axis, it's too much of a hassle to calculate where to place the line annotation for each subplot
Below is an image of what I'm trying to achive in code:

답변 (2개)

akshatsood
akshatsood 2023년 12월 10일
편집: akshatsood 2023년 12월 10일
I understand that you are seeking guidance to add a vertical line in the Z direction within the YZ view of the mesh plot. Although you have attempted line annotation, it did not seem to be a feasible solution.
To address this issue, you can utilize the "line" function to plot a vertical line at the position y=0. In the following approach, I have plotted a line while keeping the X value fixed at its maximum, ensuring it appears in front of the mesh plot. Additionally, I extended a vertical line starting from [0, 0] to [min(Z(:)), max(Z(:))]. This method fulfills the requirements mentioned in the question. Here is the code snippet for your reference
% assuming random data
[X, Y, Z] = peaks(100);
subplot(1,2,1);
mesh(X, Y, Z);
subplot(1,2,2);
mesh(X, Y, Z)
view(90,0); % YZ view
hold on;
% add vertical line centered on Y-axis
line([max(X(:)) max(X(:))], [0 0], [min(Z(:)) max(Z(:))], ...
'Color', 'r', 'LineWidth', 2, 'LineStyle', '--');
hold off;
title('YZ View');
I hope this helps.
  댓글 수: 2
Dror Hershkovitz
Dror Hershkovitz 2023년 12월 10일
Thank you! This worked perfect!
I also made a small change - just to use the xlim and zlim for the min/max values of x,z coordinated for the line, as I dont use a meshgrid for the X,Y values:, so just in case someelse stumble onthis in the future:
xLimits = xlim;
zLimits = zlim;
line([xLimits(2) xLimits(2)], [0 0], [zLimits(1) zLimits(2)], ...
'Color', 'r', 'LineWidth', 2, 'LineStyle', '--');
Dyuman Joshi
Dyuman Joshi 2023년 12월 10일
You can directly use the variables -
% assuming random data
[X, Y, Z] = peaks(100);
mesh(X, Y, Z)
view(90,0); % YZ view
hold on;
xLimits = xlim;
zLimits = zlim;
% add vertical line centered on Y-axis
line(xLimits, [0 0], zLimits, ...
'Color', 'r', 'LineWidth', 2, 'LineStyle', '--');
hold off;
title('YZ View');

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


Star Strider
Star Strider 2023년 12월 10일
I do not completely understand how you are creating the ‘YZ’ view.
If you are creating it using ‘vlew(90,0)’ the first option works, if you are creating it using ‘view(-90,0)’ the second option works. Both should automatically adapt to whatever the axil limits are.
Try these —
[X,Y,Z] = peaks(50);
figure
mesh(X, Y, Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
plot3([0 0]+max(xlim), [0 0], zlim, '--k', 'LineWidth',3)
plot3([0 0]+min(xlim), [0 0], zlim, '--k', 'LineWidth',3)
hold off
figure
mesh(X, Y, Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
plot3([0 0]+max(xlim), [0 0], zlim, '--k', 'LineWidth',3)
hold off
view(90,0)
figure
mesh(X, Y, Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on
plot3([0 0]+min(xlim), [0 0], zlim, '--k', 'LineWidth',3)
hold off
view(-90,0)
.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by