Legend in plot of data points

조회 수: 14 (최근 30일)
sna hsn
sna hsn 2023년 4월 30일
편집: Adam Danz 2023년 4월 30일
Hello everyone
I have some nodes that plot them as a 3D Plot as below:
p1=[6 3 0]';
p2=[3 5 0]';
p3=[1 7 0]';
p4=[1 -1 0]';
x=[];
y=[];
z=[];
x(1)=p1(1), y(1)=p1(2), z(1)=p1(3);
x = 6
y = 3
x(2)=p2(1), y(2)=p2(2), z(2)=p2(3);
x = 1×2
6 3
y = 1×2
3 5
x(3)=p3(1), y(3)=p3(2), z(3)=p3(3);
x = 1×3
6 3 1
y = 1×3
3 5 7
x(4)=p4(1), y(4)=p4(2), z(4)=p4(3);
x = 1×4
6 3 1 1
y = 1×4
3 5 7 -1
figure
p1=plot3(x,y,z,'rd','MarkerFaceColor','b')
p1 =
Line with properties: Color: [1 0 0] LineStyle: 'none' LineWidth: 0.5000 Marker: 'diamond' MarkerSize: 6 MarkerFaceColor: [0 0 1] XData: [6 3 1 1] YData: [3 5 7 -1] ZData: [0 0 0 0] Show all properties
grid on
xlim([-5 10])
ylim([-5 10])
zlim([-5 10])
xlabel('X Axis'), ylabel('Y Axis'), zlabel('Z Axis');
Now, I want to shape p1 as star (*) (I think it is MarkerShape but I don't know how do it for one point)
and use legend only for one point (p1) because p1 is different from others.
Thank you

채택된 답변

albara
albara 2023년 4월 30일
p1 as a star, you can use the 'Marker' property with the value '*'. To create a legend only for the first point, you can provide a label to the 'DisplayName' property. Here's the updated code:
p1 = [6 3 0]';
p2 = [3 5 0]';
p3 = [1 7 0]';
p4 = [1 -1 0]';
x = [];
y = [];
z = [];
x(1) = p1(1), y(1) = p1(2), z(1) = p1(3);
x = 6
y = 3
x(2) = p2(1), y(2) = p2(2), z(2) = p2(3);
x = 1×2
6 3
y = 1×2
3 5
x(3) = p3(1), y(3) = p3(2), z(3) = p3(3);
x = 1×3
6 3 1
y = 1×3
3 5 7
x(4) = p4(1), y(4) = p4(2), z(4) = p4(3);
x = 1×4
6 3 1 1
y = 1×4
3 5 7 -1
figure
% Plot the first point with a star marker
p1 = plot3(x(1), y(1), z(1), 'r*', 'MarkerFaceColor', 'b', 'DisplayName', 'p1');
hold on
% Plot the remaining points with a diamond marker
p_rest = plot3(x(2:end), y(2:end), z(2:end), 'rd', 'MarkerFaceColor', 'b');
grid on
xlim([-5 10])
ylim([-5 10])
zlim([-5 10])
xlabel('X Axis'), ylabel('Y Axis'), zlabel('Z Axis');
% Create a legend only for the first point
legend(p1);
This code will plot the first point (p1) with a star marker and the other points with a diamond marker. The legend will only display the label for p1.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Give me your feedback
Thanks

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by