I want to add a 45-degree line on my plot. I tried some ways (including refline) all give me a 38-degree line!

181 ビュー (過去 30 日間)
Hey all
I wanted to have a 45-degree reference line on my plot. I searched and found there is a function namely refline so I use it but as you can see here it renders me a 38-degree line:
I even used
axis equal
but another problem appears. I mean although the line converted to 45 degrees but some space appears on my graph that is not good:
Here is the code that I used:
MONTHLY = scatter(X, Y)
refline
axis equal
As a result, I want something like a black line this graph:
In order to compare trendline (red line) with the perfect condition (45-degree black line).
I attach my X and Y.
Thank you so much

採用された回答

Birdman
Birdman 2020 年 4 月 1 日
編集済み: Birdman 2020 年 4 月 1 日
Try the following:
figure(1);hold on;
MONTHLY=scatter(X,Y);
plot(0:600); %45 degree line
plot(0:600,0.73*(0:600)+6.47); %trend line
legend('X-Y Line','45 degree Line','Trend Line')
  3 件のコメント
Birdman
Birdman 2020 年 4 月 1 日
Those values are in the figure that you shared above:
BN
BN 2020 年 4 月 2 日
Dear Birdman,
Thank you so much again. Why this method does not work well on my other X and Y points? I even use Walter Roberson xlim() and ylim() suggestion but the 45-degree line won't draw correct.
Look, I use this code:
clf
figure(1);
hold on;
class1 = scatter(X,Y,12,'k','filled');
[pp,s] = polyfit(X,Y,1);
r_squared = 1 - s.normr^2 / norm(Y-mean(Y))^2;
R2 = r_squared;
plot(1:max(X),pp(1)*(1:max(X))+pp(2)); %trend line
str = {sprintf('y = %.2fx+%.2f',pp(1),pp(2)),sprintf('R^2 = %.2f',r_squared)};
annotation('textbox', [0.2, 0.75, .1, .1], 'String',str ,'FitBoxToText',...
'on','fontname','Cambria Math','HorizontalAlignment', 'center',...
'FontSize',8,'BackgroundColor', 'white');
set(gca,'fontname','Times New Roman','FontSize',8) % Set it to times
xlabel('Observed','FontSize',8)
ylabel('Modeled','FontSize',8)
box on;
grid on
plot(1:max(X),'k'); %45 degree line <<<<<<<<<<<<<<<<
xlim([0 max(X)])
ylim([0 max(Y)])
And here is the results:
Thanks.

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

その他の回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 1 日
編集済み: Ameer Hamza 2020 年 4 月 1 日
Explicitly specify slope with refline
MONTHLY = scatter(X, Y)
refline(1)
axis equal
Or draw both lines
MONTHLY = scatter(X, Y)
r1 = refline;
r1.LineWidth = 1;
r2 = refline(1);
r2.LineWidth = 1;
axis equal
  1 件のコメント
BN
BN 2020 年 4 月 2 日
Thank you so much I tried this approach but I don't know why doesnt work dor me, Here is the results:
I even use xlim and ylim
xlim([0 max(X)])
ylim([0 max(Y)])
But not works.
Thank you.

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


Kouadio Guillaume N'DRI
Kouadio Guillaume N'DRI 2022 年 1 月 22 日
編集済み: Walter Roberson 2022 年 1 月 23 日
In case someone still struggling with this. Find below my suggestion. I had the same problem with the refline.
x1=rand(1,50);
x2=rand(1,50);
scatter(x1,x2)
axis([0 max(max(x1),max(x2)) 0 max(max(x1),max(x2))])
hold on
plot([0 max(max(x1),max(x2))], [0 max(max(x1),max(x2))])

カテゴリ

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by