#Matlab画图函数及参数
学习资料方便查阅。
##plot函数
plot是绘制一维曲线的基本函数,但在使用此函数之前,我们需先定义曲线上每一点的x及y座标。下例可画出一条正弦曲线:
close all;
x=linspace(0, 2*pi, 100); % 100个点的x座标
y=sin(x); % 对应的y座标
plot(x,y);
-
若要画出多条曲线,只需将座标对依次放入plot函数即可:plot(x, sin(x), x, cos(x));
-
若要改变颜色,在座标对后面加上相关字串即可:
plot(x, sin(x), ‘c’, x, cos(x), ‘g’); -
若要同时改变颜色及图线型态(Line style),也是在座标对后面加上相关字串即可:
plot(x, sin(x), ‘co’, x, cos(x), ‘g*’);
参数设置如图:
坐标与注解
axis([xmin,xmax,ymin,ymax])函数来调整图轴的范围:
axis([0, 6, -1.2, 1.2]);
此外,MATLAB也可对图形加上各种注解与处理:
xlabel(‘Input Value’); % x轴注解
ylabel(‘Function Value’); % y轴注解
title(‘Two Trigonometric Functions’);
legend(‘y = sin(x)’,‘y = cos(x)’); % 图形注解
grid on; % 显示格线
##subplot
我们可用subplot来同时画出数个小图形於同一个视窗之中:
subplot(2,2,1); plot(x, sin(x));
subplot(2,2,2); plot(x, cos(x));
subplot(2,2,3); plot(x, sinh(x));
subplot(2,2,4); plot(x, cosh(x));
plot3(三维直线函数)
例:绘参数方程 x=t;y=sin(t);z=cos(t) 的空间曲线
clf
t=0:0.05:100;
x=t;y=sin(t);z=sin(2*t);
plot3(x,y,z,‘b:’)
###柱状图
y = [ 1.072 0.852 0.539 0.628;
3.688 3.154 1.325 1.652;
5.467 5.033 3.658 4.103];
hArray = bar(y,0.8); //0.8是柱状图宽度
ylim([-0.5 0.5 ]) //限定坐标轴的范围
x2=[’ Case 1’;'Case 2 '];
set(gca,‘XTickLabel’,x2) //设置柱状图坐标
legend(‘location’,‘a’);
string是函数名称,location不要改,a可以填northeast表示右上角,southeast表示右下角,northwest表示左上角,southwest表示左下角。