フィルターのクリア

How can I Draw a Line on a 3D plot?

190 ビュー (過去 30 日間)
Joshua Mitchell
Joshua Mitchell 2021 年 6 月 22 日
コメント済み: Bretislav Kubes 2023 年 1 月 5 日
I am trying to draw a red line across a 3D surface plot for an app:
I have the 2D plot coded to do this as follows:
surf(app.UIAxes2,Bat);
ylim(app.UIAxes2, [0 60]);
xlim(app.UIAxes2, [0 60]);
view(app.UIAxes2,2);
app.UIAxes2.PositionConstraint = 'outerposition';
xline(app.UIAxes2,value,'Color','red','LineStyle','-','LineWidth',1.5);
but the xline command will not work for the 3D plot, as it does not follow the Z data and draws a line straight through the dipole I am modelling.
"value" in the above code is whatever line is selected by the app user.
"Bat" is the matrix data I am modelling.
Does anyone know how to plot a red line like this that will contour to my Z data? Here is the code for my 3D plot below:
surf(app.UIAxes3,Bat);
ylim(app.UIAxes3, [0 60]);
xlim(app.UIAxes3, [0 60]);
view(app.UIAxes3,3);
app.UIAxes3.PositionConstraint = 'outerposition';
ztickformat(app.UIAxes3,'%g');
ax = app.UIAxes3;
ax.ZAxis.Exponent = 0;
Any help is appreciated, thank you.
  2 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 22 日
Have your tried line, instead of xline? It allows for x, y, and z input arguments.
Joshua Mitchell
Joshua Mitchell 2021 年 6 月 23 日
I am trying to use line and it seems like it is possible, but I'm having a hard time figuring out how it should work with my data specifically.
I've tried:
surf(app.UIAxes3,Bat);
ylim(app.UIAxes3, [0 60]);
xlim(app.UIAxes3, [0 60]);
view(app.UIAxes3,3);
app.UIAxes3.PositionConstraint = 'outerposition';
ztickformat(app.UIAxes3,'%g');
ax = app.UIAxes3;
ax.ZAxis.Exponent = 0;
line(app.UIAxes3,Bat(value,:),Bat(:,value),Bat(:,10),'Color','red','LineStyle','--','LineWidth',1.5);
And it is giving me strange lines like:
It is probably just how I am setting the x,y,z value but I can't seem to come up with a decent line.

サインインしてコメントする。

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 24 日
編集済み: Scott MacKenzie 2021 年 6 月 24 日
Here's an example using line. I used your code, but re-assigned ax.UIAxes2, Bat, and value so the code would execute on my machine.
UIAxes3 = gca; % your app.UIAxes3
Bat = peaks; % for testing
value = 30; % test value
surf(UIAxes3, Bat);
ylim(UIAxes3, [0 60]);
xlim(UIAxes3, [0 60]);
view(UIAxes3, 3);
UIAxes3.PositionConstraint = 'outerposition';
ztickformat(UIAxes3,'%g');
UIAxes3.ZAxis.Exponent = 0;
x = 1:size(Bat,2);
y = ones(1,size(Bat,2)) * value;
z = Bat(value,:);
line(UIAxes3,x,y,z,'Color','red','LineStyle','--','LineWidth',2.5);
  3 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 25 日
編集済み: Scott MacKenzie 2021 年 6 月 25 日
You need to change z as well, to pull z data along the y-axis instead of the x-axis:
z = Bat(:,value);
Joshua Mitchell
Joshua Mitchell 2021 年 6 月 25 日
Of course! Thank you so much, I really appreciate it.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 6 月 24 日
[X,Y,Z] = peaks(100) ;
% DRaw a line at origin
x = linspace(-3,3) ;
y = repelem(0,1,length(x)) ;
z = interp2(X,Y,Z,x,y) ;
% plot
surf(X,Y,Z)
hold on
plot3(x,y,z,'r','linewidth',3)
  1 件のコメント
Bretislav Kubes
Bretislav Kubes 2023 年 1 月 5 日
Wonderful easy solution, thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by