Clear Filters
Clear Filters

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

3 views (last 30 days)
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:

Answers (2)

akshatsood
akshatsood on 10 Dec 2023
Edited: akshatsood on 10 Dec 2023
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 Comments
Dror Hershkovitz
Dror Hershkovitz on 10 Dec 2023
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 on 10 Dec 2023
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');

Sign in to comment.


Star Strider
Star Strider on 10 Dec 2023
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)
.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!