目录
1 高阶二维图像
1.1 特殊绘图函数
函数 | 描述 |
---|---|
loglog | 双轴对数坐标刻度 |
semilogx | x轴对数坐标刻度,y轴的线性坐标刻度 |
semilogy | y轴对数坐标刻度,x轴的线性坐标刻度 |
plotyy | 左侧和右侧都有y坐标轴的图 |
函数 | 描述 |
---|---|
hist | 直方图 |
bar | 条形图 |
pie | 饼状图 |
polar | 极坐标图 |
1.2 对数图
x=logspace(-1,1,100); %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'); %x轴取对数坐标
subplot(2,2,3);semilogy(x,y);title('semilogy'); %y轴取对数坐标
subplot(2,2,4);loglog(x,y);title('Loglog'); %x,y同取对数坐标
1.3 双纵坐标图
x=0:.01:20;
y1 = 200*exp(-.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',':');
1.4 直方图
y = randn(1,1000);
subplot(2,1,1);hist(y,10); %画直方图,10直方
title('Bins = 10');
subplot(2,1,2);hist(y,50); %50直方
title('Bins = 50');
1.5 条形图
- 竖直条形图
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维条形图
- 堆叠和水平条形图
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'); %画水平直方图
1.6 饼状图
a = [10 5 20 30];
subplot(1,3,1); pie(a); %画二维饼状图
subplot(1,3,2); pie(a,[0,0,0,1]); %把a中的第四个元素的扇形隔开
subplot(1,3,3); pie3(a,[0,0,0,1]); %画三维饼状图
1.7 极坐标图
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);
1.8 梯状图和针状图
x = linspace(0,4*pi,40); y = sin(x);
subplot(1,2,1); stairs(y); %梯状图
subplot(1,2,2); stem(y); %针状图
练习1
绘制函数:,添加针状图(隔0.2一针)
x = linspace(0,10,51); y = sin(pi*x.^2/4);
t = linspace(0,10,510); f = sin(pi*t.^2/4);
hold on
plot(t,f);stem(x,y);
1.9 箱线图和误差图
load carsmall
boxplot(MPG,origin); %画箱线图
x = 0:pi/10:pi; y = sin(x);
e = std(y)*ones(size(x));
errorbar(x,y,e); %画误差线图
1.10 填充图
t = (1:2:15)'*pi/8; x = sin (t); y = cos(t);
fill(x,y,'r'); %画填充图
axis square off; %关闭坐标轴
%%在图中添加文本内容,并设置字体大小为80,加粗,位置在中央
text(0,0,'STOP','Color','w','FontSize',80,...
'FontWeight','bold','HorizontalAlignment','center');
练习2
t = (0:2:7)'*pi/4; x = sin(t); y = cos(t);
hold on
fill(x,y,'k'); %画黑底填充图
fill(x.*19/20,y.*19/20,'y'); %在黑底上加黄色填充图,让四周有一定宽度的黑色边缘
axis square off; %关闭坐标轴
%%在图中添加文本内容,并设置字体大小为80,颜色为黑色,加粗,位置在中央
text(0,0,'WAIT','Color','k','FontSize',80,...
'FontWeight','bold','HorizontalAlignment','center');
2 色彩空间
2.1 RGB色彩模式
在Matlab中,可以用[R G B]这样一个数组来表示颜色。
[0 0 0]:黑色,[1 0 0]:红色,[0 1 0]:绿色,[0 0 1]:蓝色;因此可以根据RGB三基色的色彩组合来获取想要的颜色。
练习3
%%金银铜牌数量
G = [46 38 29 24 13]; S =[29 27 17 26 8]; B=[29 23 19 32 7];
h = bar(1:5,[G' S' B']);
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals');xlabel('Country');
legend('Gold','silver','Bronze');
set(gca,'XTickLabel',{'美国','中国','英国','俄罗斯','韩国'});
添加代码,使奖牌对应的条形图具有各自奖牌的颜色:
set(h(1),'FaceColor',[225 215 0]/256);
set(h(2),'FaceColor',[192 192 192]/256);
set(h(3),'FaceColor',[255 128 0]/256);
2.2 数据可视化图像: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');
colormap(jet) %改变色彩填充方案,matlab的新版本中,默认使用parula色彩方案
imagesc(z); %颜色可以代表高度,偏蓝代表低,偏红代表高
axis square;xlabel('x');ylabel('y');
2.3 色彩条及方案
2.3.1 色彩条
colorbar; %打开色彩条
2.3.2 色彩方案
colormap(); %改变色彩方案
- Matlab内置的色彩方案:
Colormap Name | Color Scale |
---|---|
parula | |
turbo | |
hsv | |
hot | |
cool | |
spring | |
summer | |
autumn | |
winter | |
gray | |
bone | |
copper | |
pink | |
jet | |
lines | |
colorcube | |
prism | |
flag | |
white |
- 自定义色彩方案:
%创建一个元素全为1的矩阵,此方案为白色方案
a = ones(256,3); colormap(a);
练习4
创建一个自定义绿色方案,以便下面脚本的输出如下所示:
x = [1:10; 3:12; 5:14]; imagesc(x); colorbar;
a = linspace(0,1,14);b = zeros(1,14);
colormap([b' a' b']);
3 三维绘图
3.1 三维绘图函数
函数 | 描述 |
---|---|
polt3 | 三维曲线图 |
surf | 三维曲面图 |
surfc | 三维曲面下的等高线图 |
surface | 创建表面对象 |
meshc | 在网格图下绘制等高线图 |
contour | 矩阵等高线图 |
contourf | 填充二维等高线图 |
3.2 三维曲线图
例1:
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:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
grid on; axis square;
例3:
turns = 40*pi;
t = linspace(0,turns,4000); x = cos(t).*(turns-t)./turns;
y = sin(t).*(turns-t)./turns; z = t./turns;
plot3(x,y,z); grid on;
3.3 三维曲面图
- 通常用于曲面函数:
- 需要提供Matlab一组(x,y,z)点
- 使用meshgrid为在给定范围内的x和y创建网格
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); %绘制三维填充曲面图
3.4 等高线图
三维图形等高投影到二维平面上
contour(X,Y,Z); %绘制上述曲面的等高线图
axis square;
- 标注数据和填充颜色的等高线图
colormap(jet);
subplot(1,3,1);
contour(Z,[-.45:.05:.45]); axis square;
subplot(1,3,2); [C,h] = contour(Z,[-.4:.1:.4]);
clabel(C,h); %在每条等高线上标注数据
axis square;
subplot(1,3,3);
contourf(Z,[-.5:.1:.4]); %填充等高线图
axis square;
练习5
结合等高线函数生成一个图形,如下所示:
x = -2:0.2:2; y = -2:0.2:2;
[X,Y] = meshgrid(x,y); %创建网格
Z = X.*exp(-X.^2-Y.^2);
colormap(jet);
[C,h] = contourf(X,Y,Z,[-.45:.05:.4]);
clabel(C,h); axis square;
- 投影等高线图
曲面/网格和等高线的组合
x = -3.5:0.2:3.5; y = x;
[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); %曲面图加投影等高线图
3.5 视角
sphere(50); %创建半径为1的球面,由50*50个面组成
shading flat; %设置颜色着色属性为flat
light('Position',[1 3 2]); %设置光源位置
light('Position',[-3 -1 3]);
material shiny; %控制曲面和补片的反射属性为shiny
axis vis3d off; %关闭坐标轴
set(gcf,'Color',[1 1 1]); %让背景色为白色
view(-45,20); %视角可由球坐标的角度方法确定
3.6 光源
[X,Y,Z] = sphere(64); h = surf(X,Y,Z);
axis square vis3d off;
reds = zeros(256,3); reds(:,1) = (0:255)/255;
colormap(reds);
shading interp; %设置颜色着色属性为interp
lighting gouraud; %指定光照算法
set(h,'Ambientstrength',0.75,'Diffusestrength',0.5); %环境光的强度和散射光的强度
L1 = light('Position',[-1,-1,-1]);
set(L1,'Position',[-1,-1,1],'Color',[0 1 0]);
3.7 多边形patch()
- 一个包含多边形的图形对象:
%%确定8个顶点的位置,以及确定由四个顶点组成的面
v = [0 0 0;1 0 0;1 1 0;0 1 0;0.25 0.25 1;0.75 0.25 1;0.75 0.75 1;0.25 0.75 1];
f = [1 2 3 4;5 6 7 8;1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8];
%%patch是绘制多边形的函数,hsv(m)是选取m种颜色来填充,FaceColor是确定填充颜色的方式
subplot(1,2,1);
patch('Vertices',v,'Faces',f,'FaceVertexCData',hsv(6),'FaceColor','flat');
view(3); %对三维绘图使用默认的视角
axis square tight; grid on;
subplot(1,2,2);
patch('Vertices',v,'Faces',f,'FaceVertexCData',hsv(8),'FaceColor','interp');
view(3); axis square tight; grid on;