
% plot( x, y, '- b p' ,...
%
'linewidth', 1,...
%线宽
%
"MarkerEdgecolor', 'r' ,...
%描点边框颜色
%
"MarkerFacecolor ', 'y ' ,... %描点内部填充颜色
%
' Markersize', 10)
%描点大小
x = linspace(0, 2*pi, 30);
y = sin(x);
plot(x, y, '- b p', ...
'lineWidth',1, ...
'MarkerEdgeColor', 'r', ...
'MarkerFaceColor', 'y', ...
'MarkerSize', 10)

% grid on 添加网格
% grid off 取消网格
% axis on 显示坐标轴、刻度线和坐标轴标签
% axis off 关闭坐标轴、刻度线和坐标轴标签
% axis ( [xmin, xmax,ymin,ymax]) 设置x轴和y轴的显示范围
% axis equal 沿每个坐标轴使用相同的数据单位长度。
% axis square 使用相同长度的坐标轴线。相应调整数据单位之间的增量。
x = linspace(0,2*pi,100);
y = sin(x);
y2 = cos(x);
y3 = x.^2;
y4 = x.^0.5;
subplot(2,2,1);plot(x,y); grid on;
subplot(2,2,2);plot(x,y2); axis off;
subplot(2,2,3);plot(x,y3); axis ([-10, 10, 0, 100]);
subplot(2,2,4);plot(x,y4); axis equal

绘制gif动图
for i = [500, 1000, 2000, 5000, 10000]
x1 = linspace(0,1,10000);
y1 = (1 - x1.^2).^0.5;
x2 = rand([1,i]);
y2 = rand([1,i]);
count = 0;
for j = 1:i
if x2(j).^2 + y2(j).^2 <= 1
count = count + 1;
end
end
plot(x1,y1,'k.',x2,y2,".");
title(num2str(count)+" / 500 * 4 = "+num2str(count/i*4))
axis square;
frame = getframe(gcf);
%捕获坐标区或图窗作为影片帧
I = frame2im(frame);
%返回与影片帧关联的图像数据
[I,map] = rgb2ind(I,256); %将RGB图像转换为索引图像I,关联颜色图为map
if i == 500
imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
else
imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
end
end

更多二维绘图命令
errorbar 含误差图的线条
回归分析,拟合曲线的时候常用
% errorbar:含误差条的线图
% errorbar(x,y,err,["both"|"horizontal"|"vertical"])
% 绘制y对x的图,并在每个数据点处绘制一个垂直误差条,可设置误差条方向
x = linspace(0,100,10);
y = x.^0.5;
err = rand(size(x));
errorbar(x,y,err,"both");

histogram 直方图
% histogram:直方图
% histogram(x,n) 基于x创建直方图,n为区间数量
x = randn([1,10000]);
n = 100;
histogram(x,n);

scatter 散点图
% scatter:散点图
% scatter(x,y) 在向量x和y指定的位置创建一个包含圆形的散点图
% 用法类似plot
x = linspace(0, 2*pi, 30);
y = sin(x);
scatter(x, y, 'o', 'MarkerEdgeColor','b',"MarkerFaceColor",'r');

bar 柱状图
% bar(y) 创建一个条形图,y中的每一个元素对应一个条形
%
如果y是mxn矩阵,则bar创建每组包含n个条形的m个组
y = [2,3; 6,11; 23,26];
bar(y);

饼图
% pie(X,explode) 使用x中的数据绘制饼图。饼图的每个扇区代表X中的一个元素;
%
explode将扇区从饼图偏移一定的位置
X = [1 3 0.5 2.5 2];
explode = [0 1 0 1 0];
pie(X, explode);

三维绘图
plot3 三维曲线
% plot3(x1,y1,z1,LineSpec1,……,xn,yn,zn,LineSpec)
x = linspace(0, 6*pi, 200);
y = sin(x);
z = cos(x);
xlabel('x');
ylabel('sin(x)');
zlabel('cos(x)');
title('y = sin(x), z = cos(x)');
plot3(y, z, x);