How do I add a line, or a linear regression from cftool, to a scatter graph?

조회 수: 1 (최근 30일)
Roee Ben Shlomo 2018년 5월 3일
댓글: sloppydisk 2018년 5월 4일
Hi, I have a scatter graph that consists multiple arrays of information. Also I have a linear regression,which I generated its code, but when I launch the program it gives me 2 graphs: the scatter one, and the regression one in 2 different outputs. Of course I used the hold ons, both for the scatter graph (which works well) and for the regression graph, but it doesn't add it right. How do I add it together? That's how it looks:

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

답변 (1개)

sloppydisk 2018년 5월 3일
You could use copyobj:
% data
n = 200;
x = 1:n;
y = x+25*rand(1, n);
figure(1)
% handle to first axis
ax1 = gca;
scatter(y, x)
b = x'\y';
figure(2)
% handle to second plot
h2 = plot(b*x, x);
% copy line object from second plot to axis from first plot
copyobj(h2, ax1);
댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Roee Ben Shlomo 2018년 5월 4일
편집: Roee Ben Shlomo 2018년 5월 4일
Thank you very much, it works! Another thing, if I want to add another line which I have the formula for( y=-88.69x +180.5 if it matters), how do I do it? since the line I added ends when my data ends, but I want it to continue all the way.
sloppydisk 2018년 5월 4일
You can just do another plot command with new x-values. For example if you want the line from zero to five:
x = [0 5]; y = -88.69*x +180.5; plot(x, y)
Though I am sure you could have figured this out without me by reading
doc plot

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

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by