Whats wrong with this 45 degree line on the plot?

Hey all, I wanted to have a 45-degree line on my plot in order to have a background for comparing with the trend line of forecasted. I used this code in order to make this 45 degree line:
class1 = scatter(X,Y,12,'k','filled');
plot(1:max(X),'k'); %45 degree line (black)
plot(1:max(X),pp(1)*(1:max(X))+pp(2)); %trend line
In some of my data, everything is okay but in others, I achieve this figure:
I don't know why this happens. I want to have a black line in 45 degrees and trend line corresponding to it just like this picture below:
(Black is the 45-degree line and red is trend line)
Could anyone help me?
Thanks

 Accepted Answer

Appearances can be deceiving. Why do you think the line is NOT at 45 degrees? (I know, silly question, but there is a point in it.)
In fact, a 45 degree line merely means that the rise and run are the same. That is, if you change x by some number deltax, then y changes by exactly the same amount. Does that not happen in your plot? I know. You still do not see it.
So what is happening here? The units on the x and y axes are not the same. I'll create two plots as an example.
plot(rand(5),rand(5),'ko')
hold on
plot([0 1],[0 1],'r-')
axis equal
Does the line appear to be at 45 degrees? We know it is, and it looks like it is.
Now, just change the axis scaling. The numbers are still identically the same.
axis normal
axis([0 1 0 2])
Does it look like a 45 degree line now?
In this context, look at the axes on the plot you show. Are the axes scaled the same way?

3 Comments

Thank you, Since I needed to just show a line on figure I used this:
hrefline = refline([1 0]); % 45 degree refline
I found it in previous answers from you on MathWorks, I mean here. I know it seems to 38 degrees but hasn't any other options so I used it.
But I think it would be awesome if there is any way to plot y = x line on the figure just to indicate a 45-degree line on the existing figure.
Thanks again
If you just want a line from the lower left corner of the plot box to the upper right corner, you can do this:
line(xlim, ylim, 'Color', 'r');
Thank you so much

Sign in to comment.

More Answers (1)

After you plot, call
axis equal
as long as both axes have the same units, like both are mm, and NOT like x is the array index and y is in mm.

Products

Release

R2020a

Tags

Asked:

BN
on 4 Apr 2020

Commented:

BN
on 14 Apr 2020

Community Treasure Hunt

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

Start Hunting!