matlab一个figure画8个散点图及回归线的代码
时间: 2024-05-11 17:13:58 浏览: 52
以下是一个示例代码,用于在一个figure中绘制8个散点图和回归线:
```matlab
% 生成数据
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(x) + cos(x);
y4 = 2*sin(x);
y5 = 2*cos(x);
y6 = 2*sin(x) + cos(x);
y7 = 3*sin(x);
y8 = 3*cos(x);
% 绘制8个散点图和回归线
figure;
subplot(2,4,1);
scatter(x, y1);
hold on;
p1 = polyfit(x, y1, 1);
plot(x, polyval(p1, x));
title('y = sin(x)');
subplot(2,4,2);
scatter(x, y2);
hold on;
p2 = polyfit(x, y2, 1);
plot(x, polyval(p2, x));
title('y = cos(x)');
subplot(2,4,3);
scatter(x, y3);
hold on;
p3 = polyfit(x, y3, 1);
plot(x, polyval(p3, x));
title('y = sin(x) + cos(x)');
subplot(2,4,4);
scatter(x, y4);
hold on;
p4 = polyfit(x, y4, 1);
plot(x, polyval(p4, x));
title('y = 2sin(x)');
subplot(2,4,5);
scatter(x, y5);
hold on;
p5 = polyfit(x, y5, 1);
plot(x, polyval(p5, x));
title('y = 2cos(x)');
subplot(2,4,6);
scatter(x, y6);
hold on;
p6 = polyfit(x, y6, 1);
plot(x, polyval(p6, x));
title('y = 2sin(x) + cos(x)');
subplot(2,4,7);
scatter(x, y7);
hold on;
p7 = polyfit(x, y7, 1);
plot(x, polyval(p7, x));
title('y = 3sin(x)');
subplot(2,4,8);
scatter(x, y8);
hold on;
p8 = polyfit(x, y8, 1);
plot(x, polyval(p8, x));
title('y = 3cos(x)');
```
这个代码将生成一个2x4的subplot,每个subplot中包含一个散点图和一个回归线。可以根据需要修改数据和subplot的布局。
阅读全文