How do you plot markers to specific points?

조회 수: 97 (최근 30일)
DJ Mckenna
DJ Mckenna 2023년 5월 16일
댓글: DJ Mckenna 2023년 5월 16일
I have the plot markers added, but they aren't where I need them. They need to be on each line at x= 1,5,9, and 15. How do I achieve this?
  댓글 수: 2
VBBV
VBBV 2023년 5월 16일
편집: VBBV 2023년 5월 16일
Oneway you can plot markers at specfic points (without using find function) is by creating vector separately and evaluating the equations at those points. Later plot them using hold on
clearvars
x = linspace(1,15,100);
y1 = log(x.^2+4)./(sqrt(2.*x.^2+1));
y2 = sin(x)./log(x.^2)
y2 = 1×100
Inf 3.4370 1.9248 1.3987 1.1153 0.9263 0.7827 0.6638 0.5596 0.4651 0.3772 0.2947 0.2168 0.1431 0.0738 0.0090 -0.0511 -0.1059 -0.1552 -0.1988 -0.2361 -0.2672 -0.2917 -0.3095 -0.3208 -0.3255 -0.3239 -0.3162 -0.3027 -0.2840
% Marker plot
xM = [1 5 9 15];
y3 = log(xM.^2+4)./sqrt(2.*xM.^2+1);
y4 = sin(xM)./log(xM.^2)
y4 = 1×4
Inf -0.2979 0.0938 0.1201
figure
hold on
plot(x,y1,x,y2,'--')
plot(xM,y3,'b+',xM,y4,'r+','MarkerSize',12); grid
DJ Mckenna
DJ Mckenna 2023년 5월 16일
Thanks a lot!

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

답변 (2개)

Torsten
Torsten 2023년 5월 16일
편집: Torsten 2023년 5월 16일
The points in the graph you address are x(1), x(5), x(9) and x(15), not x=1, x=5, x=9 and x=15.
Use "find" before plotting to get the respective indices or calculate them by hand.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 5월 16일
편집: Dyuman Joshi 2023년 5월 16일
OP will not obtain the exact values with the current data, as the values 5 and 9 don't exist in x.
The best idea would be use the colon operator to define x -
x = linspace(1,15,100);
min(abs(x-9))
ans = 0.0606
min(abs(x-5))
ans = 0.0404
%create x via colon operator
x0 = 1:0.01:15; %Ideally x0 = (100:1:1500)/100;
min(abs(x0-9))
ans = 0
min(abs(x0-5))
ans = 0

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


Walter Roberson
Walter Roberson 2023년 5월 16일
You have the potential problem that you might not have x coordinates that are exactly 5 or 9 (but exactly 1 and exactly 15 should exist because of the way you linspace()). So if you want markers at x = 5 and x = 9, you have to interpolate positions. The alternative would be to locate the locations in x that are closest to the desired values.
marker_x = [1, 5, 9, 15];
marker_y1 = interp1(x, y1, marker_x);
marker_y2 = interp1(x, y2, marker_x);
h = plot(x, y1, '-', x, y2, '--');
hold on
plot(marker_x, marker_y1, '+', 'Color', h(1).Color);
plot(marker_x, marker_y2, '+', 'Color', h(2).Color);
hold off

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by