目标:
- 画2D图
- 颜色空间
- 3D画图
图的选择索引:
一、画2D图
Special Plots:
相关的函数:
1. Logarithm Plots:关于对数函数的画图
- semilogx():对x轴取log
- semilogy():对y轴取log
- loglog():对x轴和y轴都取log
x = logspace(-1,1,100);
y = x.^2;
subplot(2,2,1);
plot(x,y) % 线性画图
title('Plot');
subplot(2,2,2);
semilogx(x,y); % 对x轴取对数
title('Semilogx');
subplot(2,2,3);
semilogy(x,y); % 对y轴取对数
title('Semilogy');
subplot(2,2,4);
loglog(x,y); % 对x和y轴都取对数
title('Loglog');
set(gca,'XGrid','on')
- plotyy():画两个y轴的图像,返回三个handle(AX:axes,AX(1)和AX(2),H1:y1画出的先,H2:y2画出的线)
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2);
set(get(AX(1),'Ylabel'),'String','Left Y-axis')
set(get(AX(2),'Ylabel'),'String','Right Y-axis')
title('Labeling plotyy')
set(H1,'LineStyle','--');
set(H2,'LineStyle',':');
2. Histogram:柱状图
hist(data,组数):查看数据整体分布
y = randn(1,10000);
subplot(2,1,1);
hist(y,10);
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');
bar():查看数据的个体分布
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,3,1); bar(x); title('A bargraph of vector x');
subplot(1,3,2); bar(y); title('A bargraph of vector y');
subplot(1,3,3); bar3(y); title('A 3D bargraph'); % 3维展示
Stacked and Horizontal Bar:堆积和水平展示
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,2,1);
bar(y,'stacked'); %堆积展示
title('stacked');
subplot(1,2,2);
barh(y); % 水平展示
title('Horizontal');
3. pie:饼状图
a = [10 5 20 30];
subplot(1,3,1); pie(a);
subplot(1,3,2); pie(a,[0,0,1,1]); % 为1的位置饼图被分开
subplot(1,3,3); pie3(a,[0,0,0,1]); % 立体图形展示
4. polar:极坐标,利用角度来画图
x = 1:100; theta = x/10; r = log10(x);
subplot(1,4,1); polar(theta,r);
theta = linspace(0, 2*pi); r = cos(4*theta);
subplot(1,4,2); polar(theta,r);
theta = linspace(0, 2*pi, 6); r = ones(1,length(theta));
subplot(1,4,3); polar(theta,r);
theta = linspace(0, 2*pi); r = 1-sin(theta);
subplot(1,4,4); polar(theta, r);
5. staris and stem Charts: 类似于楼梯和柱子
x = linspace(0, 4*pi, 40);
y = sin(x);
subplot(1,2,1);stairs(y); % 阶梯
subplot(1,2,2);stem(y); % 圆圈线
6. Boxplot and Error Bar
% boxplot
load carsmall
boxplot(MPG, Origin)
% errorbar
x = 0:pi/10:pi; y = sin(x);
e = std(y)*ones(size(x));
errorbar(x,y,e);
7. fill():绘图函数,填满颜色
t = (1:2:15)'*pi/8; x = sin(t); y = cos(t);
fill(x,y,'r');axis square off;
text(0, 0, 'STOP', 'Color', 'w', 'FontSize', 80,...
'FontWeight','bold','HorizontalAlignment','center');
二、Color Space(颜色空间)
- [ R G B]:可以用0-1表示,也可以用0-255表示
- 0 是最小值,黑色
- 1 是最大值,白色
- 8-bit 等值的:
1. VIsualizing Data as An image:imagesc()
[x, y] = meshgrid(-3:.2:3,-3:.2:3);
z = x.^2 + x.*y + y.^2;
% 画图
surf(x, y, z);
box on;
set(gca, 'FontSize', 16);
zlabel('z');
xlim([-4 4]);
xlabel('x');
ylim([-4 4]);
ylabel('y');
% 二维热力图,用颜色表示z轴
imagesc(z);
axis square;
xlabel('x');
ylabel('y')
% 下面的三个命令要分别执行
% 添加颜色深浅标签
colorbar;
% 热色系
colormap(hot);
% 冷色系
colormap(cool);
% 灰度
colormap(gray);
相关颜色:
三、3D绘图
相关3D绘图函数
1. plot3():
x = 0:0.1:3*pi;
z1 = sin(x);
z2 = sin(2*x);
z3 = sin(3*x);
y1 = zeros(size(x));
y3 = ones(size(x));
y2 = y3./2;
plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g');grid on;
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
2. Principles for 3D Surface Plots:
- 通常需要画函数:z = f(x, y)
- 需要提供给matlab一个(x, y, z)的要给集合
- 使用 meshgrid(网格)创建关于X和Y的矩阵给定一个范围
% 生成一个x和y的网格
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y);
mesh() and surf():
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X, Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
subplot(1,2,1); mesh(X,Y,Z);
subplot(1,2,2); surf(X,Y,Z);
contour():将3D图像投影到2D
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X, Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
subplot(2,1,1); mesh(X,Y,Z); axis square;
subplot(2,1,2); contour(X,Y,Z); axis square;
Various Contour Plots:
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X, Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
% 显示更多的线
subplot(1,3,1); contour(Z,[-.45:.05:.45]); axis square;
subplot(1,3,2); [C, h] = contour(Z);
% 给线标数值
clabel(C, h); axis square;
% 填充颜色
subplot(1,3,3); contourf(Z); axis square;
meshc() and surfc():
x = -3.5:0.2:3.5;
y = -3.5:0.2:3.5;
[X, Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
% 直接在图的下方显示出等值线
subplot(1,2,1); meshc(X,Y,Z);
subplot(1,2,2); surfc(X,Y,Z);
View Angle:view()
- 变化看图的角度
sphere(50); shading flat;
light('Position', [1 3 2]);
light('Position', [-3 -1 3]);
material shiny;
axis vis3d off;
set(gcf,'Color',[1 1 1]);
view(-45,20);
Light:light() 打光
[X, Y, Z] = sphere(64); h = surf(X, Y, Z);
axis square vis3d off;
reds = zeros(256, 3); reds(:, 1) = (0:256.-1)/255;
colormap(reds); shading interp; lighting phong;
set(h, 'AmbientStrength', 0.75, 'DiffuseStrength', 0.5);
L1 = light('Position',[-1, -1, -1]);
% 改变打光的位置
set(L1, 'Position', [-1, -1, 1]);
patch():画多边形