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

106 次查看(过去 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 个评论
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
编辑: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 CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by