필터 지우기
필터 지우기

How to add a 1:1 reference line

조회 수: 77 (최근 30일)
Ellen Maas
Ellen Maas 2020년 11월 13일
댓글: Star Strider 2020년 11월 14일
Hi, I am trying to add a dashed 1:1 reference line to subplots of data correlations. The range of values is the same for both x and y.
I have tried refline, but it appears to be tied to the range of the data, such that it only extends across the plot where there are data points.
For example:
In the plot above, the 1:1 line stops short of the lower-left and upper-right corners. I want a line that goes all the way from the point of origin to the upper right-hand corner of the plot, such as in this plot:
It only works in this graph because there are data points closer to the corners. I want a line that is not dependent on the data, like xline or yline, but has a slope of 1.
Any help is greatly appreciated!

채택된 답변

Star Strider
Star Strider 2020년 11월 14일
I am not certain what you want.
Try this:
x = rand(1,10); % Create Data
y = rand(1,10); % Create Data
figure
plot(x, y, '.')
hold on
plot(xlim, ylim, '--r')
hold off
legend('Data', 'Reference Line', 'Location','SE')
.
  댓글 수: 5
Ellen Maas
Ellen Maas 2020년 11월 14일
So I just tried this, and it works for all the graphs and axis ranges:
plot([0 ylim*2], [0 ylim*2], '--r')
Now the line extends all the way across. None of my axes are less than 0, so having 0 as the minimum value will not cause any problems.
Thank you for your help in thinking this through!
Star Strider
Star Strider 2020년 11월 14일
As always, my pleasure!
If any are less than 0, an extension of this idea will work similarly:
plot([min([xlim ylim]) max([xlim ylim])], [min([xlim ylim]) max([xlim ylim])], '--r')
That generalises it to any range.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by