Whats wrong with this 45 degree line on the plot?

16 views (last 30 days)
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

John D'Errico
John D'Errico on 4 Apr 2020
Edited: John D'Errico on 4 Apr 2020
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
Image Analyst
Image Analyst on 7 Apr 2020
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');

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Apr 2020
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.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!