필터 지우기
필터 지우기

Is there a way to put a marker on all of the points where y=0 on a plot?

조회 수: 21 (최근 30일)
Maddie Sanders
Maddie Sanders 2020년 2월 16일
댓글: Sindar 2020년 2월 16일
I have a plot and would like to highlight/mark the points where y=0. Is there anyway to do this without knowing the x-value? Also is there anyway to label these points with the x and y value on the plot?
  댓글 수: 2
Sindar
Sindar 2020년 2월 16일
Do you have the data used to create the plot?
Walter Roberson
Walter Roberson 2020년 2월 16일
Note that the above will only work for y values that are exactly 0. Depending on your needs, you might want to test for "near" zero, such as
idx = abs(y) < 1e-6;
But it is also common to have a vector of x and y values where lines joined between them cross y = 0 somewhere rather than very close to an associated x value. In such cases, you may have to interpolate to figure out where the zero is (if all you have is data) or you may need to fzero() to find the zero (if you have a function).

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

답변 (1개)

Sindar
Sindar 2020년 2월 16일
Assuming you have access to the data (x,y) which generated the plot:
% generate sample x,y
x=1:1000;
% generate 1000 random integers between -5 and 5
y=randi([-5,5],1000)
% plot the data as a black line
plot(x,y,'k')
% find indices where y is 0
idx = (y==0);
% add to the existing plot, with red asterisks where y is zero
hold on
myplot = plot(x(idx),y(idx),'*r')
% with Matlab 2019b, you can programmatically label these points
for ind=idx
datatip(myplot,x(ind),y(ind))
end
  댓글 수: 1
Sindar
Sindar 2020년 2월 16일
you may want to add a tolerance if your data is not generated in a way where it will be exactly zero:
idx = ( abs(y) < 1e-10 );

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by