MATLAB学习日记2 简单绘图
1、plot的风格,线条颜色、点的形状、线条样式(顺序为color、line、Data)
plot(x,y,‘str’)
示例
x=0:1:8*pi;
y=cos(x);
z=-log(x);
h=exp(-2.*x);
plot(x,y,'r--X',x,z,'c-*',x,h,'b:o');
运行
2、图形标注x轴、y轴、title、legend
xlabel();
ylabel();
title();
legend();
xlabel('x = 0 to 8*pi');
ylabel('values of cos(x) and -log(x)');
title('Function Plots of cos(x) and -log(x)');
legend('cos(x)','-log(x)');
运行结果
3、标注、箭头
text,利用LaTeX显示文本格式的公式
annotation,显示箭头
x = linspace(0,3);
y = x.^2.*sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);//画直线,x的范围(2,2)也就是点2,y的范围是0到y(2)
str = '$$ \int_{0}^{2} x^2\sin(x) dx $$'; //这是标注的文本格式,利用latex显示
text(0.25,2.5,str,'Interpreter','latex');//text的固定格式
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]); //箭头与x、y的范围
运行
4、 Identifying the Handle of An Object
function
二、得到图的数据之后,可以对图进行修改
1.比如,修改线的limit
set(gca,'Xlim',[0,2*pi]);
set(gca, 'YLim', [-1.2, 1.2]);
或者
xlim([0, 2*pi]);
ylim([-1.2, 1.2]);
2.修改figure的值
set(gca, 'FontSize', 25);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', 0:90:360);
set(gca, 'FontName', 'symbol');
set(gca, 'XTickLabel', {'0', 'p/2', 'p', '3p/2', '2p'});
3.修改Marker的状态
plot(x,'-md','MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
4.多图如何显示(图像处理常用)
召唤多图用,figure
figure, plot(x,y1);
figure, plot(x,y2);
多图同时显示用,subplot
subplot(m, n,1);
subplot(1,2,1);plot(x,y1);
subplot(1,2,2); plot(x,y2);
保存运行图片用,saveas
saveas(gcf,'<filename>','<formattype>');
三、绘制多样图
1、有关log的特殊的绘图
x=logspace(-1,1,100); //在x取值在10^-1~10^1,取100点
y=x.^2;
subplot(2,2,1);
plot(x,y);
title('Plot');
subplot(2,2,2);
semilogx(x,y);
title('semilogx');
subplot(2,2,3);
semilogy(x,y);
title('Semilogy');
subplot(2,2,4);
loglog(x,y);
title('Loglog');
set(gca,'XGrid','on');//在x轴上画网格线
运行
2、将两个图画在一起,并且y轴放置不同的函数,plotyy(x,y1,x,y2);
x=0:0.01:20;
y1=200*exp(-0.5*x).*sin(x);
y2=-0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2]=plotyy(x,y1,x,y2); //[AX,H1,H2] = plotyy(...) returns the handles of the two axes created in AX and the handles of the graphics objects from each plot in H1 and H2. AX(1) is the left axes and AX(2) is the right axes.
set(get(AX(1),'Ylabel'),'String','Left Y-axis')
set(get(AX(2),'Ylabel'),'String','Right Y-axis')
title('Labeling ploty');
set(H1,'Linestyle','--'); //set的用法set(H,'PropertyName',PropertyValue)
set(H2,'Linestyle',';');
运行结果
3、生成数据矩形图,Histogram
hist(x,m);
y = randn(1,1000); //生成一个1*1000的随机矩阵
subplot(2,1,1);
hist(y,10); //将生成的矩阵元素等分成十份并生成相应的直方图columns
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');
4、生成柱状图,bar graph
bar(x);
bar(y);
bar3(y);
x=[1 2 5 4 8];
y=[x;1:5];
subplot(1,3,1);
bar(x); //生成有关x的直方图
title('A bargraph of Vector x');
subplot(1,3,2);
bar(y); //生成 x与y 的直方图
title('A bargraph of Vector y');
subplot(1,3,3);
bar3(y); 生成x与y的3d图
title('A 3D bargraph');
PS:如何改变柱状图的填充颜色
handle = bar(Y);
set(handle, 'facecolor', [255/255 0/255 0/255]);
运行
5、生成饼状图,pie chair
pie(a);
pie(a,[0 0 0 ]);
pie3(a);
a=[21 15 34 56 78 ];
subplot(1,3,1);
pie(a);
title('饼状图');
subplot(1,3,2);
pie(a,[0 0 0 1 0]); //0表示拼接在一起,1表示分块
title('分块饼状图');
subplot(1,3,3);
pie3(a,[1 1 1 1 1 ]);
title('分块3D图');
6、生成polar chart 极坐标图
theta 表示角度
r表示半径
theta = linspace(0,2*pi,7); //linspace表示 在0-2pi之间,均等分八份
r =ones(1,length(theta)); //ones(m,n);表示生成一个m*n的矩阵,且矩阵的元素均为1
//length(x),表示返回x的值
polar(theta,r);
**
PS:
1、plot(x,y);与plot(x,y);的区别
x坐标不同。
第一个是以参数x为横坐标
第二个以默认的1,2 ,3,4 为坐标值。个数为你的数据个数。
2、bar(x,y);生成xy的矩阵
bar(Y) 生成1y的矩阵
7、Color Space
[R G B]分别对应一个[255 255]的矩阵
8、画三维图像
plot3(x,y,z);
grid on;