문제

I have the following code :

clc
clear
x=[1 2.5 2 3 5 6 3.5 2.1 4 .5]
y=[1 3 1.5 2 1.4 5 3.8 2.1 3 3.5]
p=plot(x,y,'r.')
set(p,'MarkerSize',30)
reg=polyfit(x,y,2)
p2=plot(reg)

how can I show both plot in the same figure and also I want axis range to be between 0 and 10.

도움이 되었습니까?

해결책

I think this is what you really want to do:

x=[1 2.5 2 3 5 6 3.5 2.1 4 .5];
[x, inds] = sort(x);
y=[1 3 1.5 2 1.4 5 3.8 2.1 3 3.5];
y = y(inds);

p=plot(x,y,'r.');
set(p,'MarkerSize',30)
set(gca,'XLim',[0 10])

reg=polyfit(x,y,2);
hold on
plot(x, polyval(reg, x))

다른 팁

See http://www.mathworks.com/help/matlab/ref/hold.html

"hold on;"

hold on retains the current graph and adds another graph to it. MATLAB adjusts the axes limits, tick marks, and tick labels as necessary to display the full range of the added graph.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top