Matlab学习笔记(二)绘图

hold on
plot(cos(0:pi/20:2*pi),"go-");
plot(sin(0:pi/20:2*pi),"rx-");
legend("cos(0:pi/20:2*pi)","sin(0:pi/20:2*pi)");
ylabel("ylabel");
xlabel("xlabel");
hold off

matlab每次绘图都会清楚原来的图形,想要把两次绘图的结果显示在一起需要用hold on/off指令

image

x=linspace(0,3);
y=x.^2.*sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);
str = '$$\int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);

image

y= [75 91 105 123.5 131 150 179 203 226 249 281.5];
x = 2000:2010;
bar(x,y);

image

theta = 0:pi/50:6*pi;
x=cos(theta);
y=sin(theta);
z=0:300;
plot3(x,y,z);

image

x = -4:0.1:4;
y1 = cos(x);
y2 = cos(2.*x);
y3 = cos(4.*x);

subplot(2,2,1);
plot(x,y1);
title('y=cos(x)');

subplot(2,2,2);
plot(x,y2);
title('y=cos(2x)');

subplot(2,2,[3,4]);
plot(x,y3);
title('y=cos(4x)');

image

x=-3:0.1:3;
y=-3:0.1:3;
[X,Y]=meshgrid(x,y);
Z=X.^2+Y.^2;
surf(X,Y,Z);

image

t = 0:pi/50:2*pi;
X=cos(t);
Y=sin(t);

plot(X,Y);
hold on;
axis equal;

lineX=[0,1];
lineY=[0,0];
h=plot(lineX,lineY);

theta = 0;
while true
    theta=theta+0.05;
    lineX(2)=cos(theta);
    lineY(2)=sin(theta);
    set(h,'XData',lineX,'YData',lineY);
    drawnow;
end

在matlab中圆中的半径会绕着中心旋转。

image

样式设置

一张图由画布、坐标轴、图形组成。

h=plot(x,y);
get(h);

画好一幅图后可以用get()获取图形的样式。

get(gca);
get(gcf);

gcf,gcf代表画布、坐标轴

设定样式

set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1,1]);
figure;

figure命令可以开启另一张画布。

x = logspace(-1,1,100);
y = x.^2;
subplot(2,2,1);
plot(x,y);
subplot(2,2,2);
semilogx(x,y);
subplot(2,2,3);
semilogy(x,y);
subplot(2,2,4);
loglog(x,y);

subplot命令可以将多个figure放到一张图中

image

posted @ 2024-04-10 01:54:55
评论加载中...
发表评论