How to add vertical line in z direction in YZ view meshplot?
6 次查看(过去 30 天)
显示 更早的评论
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:
0 个评论
回答(2 个)
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 个评论
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
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)
.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!