MATLAB 图形基础
MATLAB? 基础与编程入门
2
本章概述
2-D 绘图
图形标注
保存和输出图形
数据插值和曲线拟合
3-D 绘图
专用绘图程序
子图
选择轴系
图像和 colormaps
参考,特殊图形标注( LaTeX)
3
确定 x轴和 /或 y轴数据
确定颜色、线型和标识符号
(如果未指定 ‘clm’,采用默认值 )
语法,
绘制一条曲线,
绘制多条曲线,
plot(xdata,ydata,'color_linestyle_marker')
2-D 绘图
plot(x1,y1,'clm1',x2,y2,'clm2',...)
4
绘图示例 (结果 )
TITLE
TEXT
或
GTEXT
XLABEL
YLABEL
LEGEND
曲线
5
创建一条蓝色正弦波曲线
>> x = 0:.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> plot_2d
6
添加网格
GRID ON 命令在当前图形中 添加网格
GRID OFF 命令关闭当前图形网格
GRID 命令在网格状态间切换
>> grid on
7
在图形中增加其他曲线
HOLD ON 锁定当前的图形
HOLD OFF 释放当前图形
HOLD 在锁定状态间切换
>> addgraph
>> x = 0:.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> grid on
>> hold on
>> plot(x,exp(-x),'r:*')
8
控制视图区域在命令行,
ZOOM ON 打开视图区域选择观察功能
ZOOM OFF 关闭缩放功能
ZOOM 切换缩放操作状态
AXIS 设置轴坐标范围
[xmin xmax ymin ymax]
>> axis([0 2*pi 0 1])
ZOOM 使用图形窗口工具栏
9
图形标注
>> annotation
>> title('2-D Plots')
>> xlabel('Time')
>> ylabel('Sin(t)')
>> text(pi/3,sin(pi/3),...
'<--Sin(\pi/3)')
>> legend('Sine Wave',...
'Decaying Exponential')
10
特殊图形标注 (TeX)
上标和下标:
super and sub
字体:
Some boldface and some italics
希腊字符,
y = e?xsin(βx)
对 {}里面的注释内容有效,直至被修改或删除 ( \rm )
>> latex_examp
>> text(3.5,20,'y = e^{\alphax}sin(\betax)')
>> ylabel('Some \bfboldface\rm and some \it{italics}')
>> xlabel('^{super} and _{sub}')
参考,Special characters using LaTeX
11
>> curve_examp
%选择“旋转”按钮
%旋转所生成的曲面
%旋转是绘图编辑工
%具条上的选项之一绘图编辑工具栏
12
属性编辑器
图形编辑:
在窗口工具栏内点击图形对象编辑按扭
>> plotedit
当图形处于编辑状态时可以激活属性编辑器,
双击图形对象
鼠标右键点击目标并选择,Properties”选项
>> propedit
交互式绘图工具
>>plottool
13
属性编辑器 — Axis
14
删除 /拷贝当前对象
激活绘图编辑器并选择对象 (正弦波曲线 )
Ctrl-x - 剪切对象
Ctrl-c - 复制对象
Delete - 删除对象
15
练习,使用 2-D 绘图首先,装载数据。这是 10年内 Boston,MA和 Naples,FL每年的房价:
>> load reloc_data
在内存中,有一个变量名为 relo_data的 11x3 的矩正。
这个矩正有以下三列,
从 1990到至今的年份数
在 Boston,MA,有两间卧室房子的平均价格
在 Naples,FL,有两间卧室房子的平均价格使用 MATLAB的 2-D 绘图能力,绘制数据图并比较,
在同一个 figure窗口下绘制两个曲线图,每一个曲线图包括对应年份的房屋价格,
增加标题、图例以及坐标轴标签,
坐标轴标签使用 14号字,
16
解答:使用 2-D绘图
>> load reloc_data
>> whos
>> numyears=relo_data(:,1);
>> Bosprices=relo_data(:,2);
>> Naprices=relo_data(:,3);
>> plot(numyears,Bosprices)
>> hold on
>> plot(numyears,Naprices,'r:*')
>> title('Housing prices in Boston,MA versus Naples,FL')
>> xlabel('\fontsize{14}{Number of Years}')
>> ylabel('\fontsize{14}{Price Ranges}')
>> legend('Boston','Naples')
>> reloc_soln
17
保存和打开图形
>> open('plot_3d.fig')
18
把图形导出到文件部分支持的格式:
JPEG images (*.jpg)
Bitmap files (*.bmp)
TIFF images (*.tif)
EPS files (*.eps)
等等,…
19
从命令行导出图形用 PRINT命令生成硬拷贝输出,
语法,
仅用 PRINT命令把当前图形传送到当前打印机,
>> print
指定打印设备 (比如,以 TIFF或 PostScript 格式输出,或是控制输出到打印机内容的打印驱动设备 ):
>> print -device
>> print -device -options
指定一个文件名把输出直接输出到指定文件而不是打印机,
>> print -device -options filename
PRINTOPT 是一个 M文件,用户或系统管理员可以编辑它,并且可以确定缺省的打印机类型和目标文件,调用它时返回缺省值:
>> [pcmd,dev] = printopt >> help print
>> help printopt
20
拷贝到剪贴扳拷贝到剪贴板,
选项,
(Edit / Copy Options)
拷贝,
(Edit / Copy Figure)
0 1 2 3 4 5 6
0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
1
< - - S i n ( p / 3 )
2 - D P l o t s
S
i
n
(
t
)
T i m e
S i n e W a v e
D e c a y i n g E x p o n e n t i a l
21
拷贝选项
22
数据插值
INTERP1() - 1-D插值 (查表 )
INTERP2(),INTERP3(),INTERPN(),....
yi = interp1(x,y,xi,method)
>> interp_ex
23
求满足这些方程的斜率和截距
(y = mx + b)
示例:使用“左除”作一阶线性拟合
>> x = [2 1; 3 1; -3 1];
>> y = [12; 17; -5];
>> mb = x\y
mb =
3.5806 %Slope
5.6129 %Intercept
% Now evaluate y = mx + b
>> y_fit = mb(1)*x(:,1) + mb(2)
y_fit =
12.7742
16.3548
-5.1290
12 = m*2 + 1*b
17 = m*3 + 1*b
-5 = m*-3 + 1*b
>> solve_examp2
24
多项式拟合
POLYFIT - 多项式数据拟合
POLYVAL - 多项式计算
Left Division>> x = 0:.1:10;
>> y = sin(x)+cos(2*x);
>> k5 = polyfit(x,y,5);
>> y5 = polyval(k5,x);
>> k11 = polyfit(x,y,11);
>> y11 = polyval(k11,x);
>> plot(x,y,'g',...
x,y5,'r',...
x,y11,'b')
>> curve_fit
25
基本拟合工具
26
基本拟合工具,显示拟合点
27
练习:多项式拟合从载入数据开始,
>> load dessertdata
两个 1X10向量,温度和价格,将被载入到 workspace。 温度向量包含冰淇淋蛋筒每种价格下对应的温度。
使用基本拟合工具箱对数据进行四阶和五阶拟合
把系数值以结构体数组形式保存到工作区
也可以使用 POLYFIT 和 POLYVAL命令来完成这项工作。
附加题当使用更高阶次的多项式去拟合数据时发生了什么?
28
解答:多项式拟合
29
解答,在命令行进行多项式拟合
load dessertdata %载入甜点的绘图数据
plot(temps,prices)
hold on
k4 = polyfit(temps,prices,4); %四阶拟合
ft4 = polyval(k4,temps); %求解多项式
plot(temps,ft4,‘r’) %绘图
k5 = polyfit(temps,prices,5); %5阶拟合
ft5 = polyval(k5,temps); %求解多项式
plot(temps,ft5,‘k’) %绘图
xlabel('Temperature (degrees F)')
ylabel(‘Prices (cents)')
title(‘Price change for ice pops on a July day')
legend('Temperature Data',‘Fourth Order Fit',...
‘Fifth Order Fit',2)
>> trendfit_soln
30
解决方法,多项式拟合 (续 )
>> trendfit_soln
附加题当采用高于 6阶的式子拟合时会发生什么?
会得到警告信息当试图对一个很接近奇异 (不可逆 )的矩阵求逆时就会发生这样的情况,
这可能是由于使用阶数过高的多项式拟合数据,
想象一下用一个巨大的锤子去挂一幅油画时会发生些什么,
Warning,Polynomial is badly conditioned,Remove repeated data points
or try centering and scaling as described in HELP POLYFIT.
(Type "warning off MATLAB:polyfit:RepeatedPointsOrRescale" to
suppress this warning.)
> In C:\MATLAB6p5\toolbox\matlab\polyfun\polyfit.m at line 75
31
数据统计工具
>> load census
>> plot(cdate,pop,'*')
32
3-D 曲线绘图参考,Color,Linestyle,Marker options
>> z = 0:0.1:40;
>> x = cos(z);
>> y = sin(z);
>> plot3(x,y,z)
plot3(xdata,ydata,zdata,'clm',...)
>> plot_3d
33
3-D 曲面绘图
>> surf_3d
34
专用绘图语句
>> spec_plots
35
专用绘图语句 (续 )
>> spec_plots2
36
子图
SUBPLOT- 在同一图形窗口中显示多个图形
>> subplotex
subplot(#rows,#cols,index)
>> subplot(2,2,1);
>> plot(1:10)
>> subplot(2,2,2)
>> x = 0:.1:2*pi;
>> plot(x,sin(x))
>> subplot(2,2,3)
>> x = 0:.1:2*pi;
>> plot(x,exp(-x),'r')
>> subplot(2,2,4)
>> plot(peaks)
37
子图的使用
>>usingsubplots
38
不同的坐标轴刻度
SEMILOGY
Y轴线性
X轴对数
PLOTYY
2 组线性轴
LOGLOG
两轴均为对数刻度
SEMILOGX
X轴对数
Y轴线性
>> other_axes
39
图像 减少内存的占用量,
图像以 UINT8 - 1字节方式代表>> a = magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> image(a);
>> map = hsv(16)
map =
1.0000 0 0
1.0000 0.3750 0
1.0000 0.7500 0
.....
>> colormap(map) >> imagex
用 colormap
第 2行数据画像素点 l (1,2)
第 2行
40
Colormap 编辑器
>> colormapeditor
当前 colormap
节点颜色设置,其它颜色用内插的方式得到双击节点,设定其颜色
41
示例,图像( images)和调色板( Colormaps)
>> draw_cape
>> load cape
>> image(X)
>> colormap(map)
MathWorks 公司
42
本章小结
2-D 绘图
图形标注
保存和导出图形
插值和曲线拟合
3-D 绘图
专用绘图指令
子图
坐标轴设置
图像和 colormap
MATLAB? 基础与编程入门
2
本章概述
2-D 绘图
图形标注
保存和输出图形
数据插值和曲线拟合
3-D 绘图
专用绘图程序
子图
选择轴系
图像和 colormaps
参考,特殊图形标注( LaTeX)
3
确定 x轴和 /或 y轴数据
确定颜色、线型和标识符号
(如果未指定 ‘clm’,采用默认值 )
语法,
绘制一条曲线,
绘制多条曲线,
plot(xdata,ydata,'color_linestyle_marker')
2-D 绘图
plot(x1,y1,'clm1',x2,y2,'clm2',...)
4
绘图示例 (结果 )
TITLE
TEXT
或
GTEXT
XLABEL
YLABEL
LEGEND
曲线
5
创建一条蓝色正弦波曲线
>> x = 0:.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> plot_2d
6
添加网格
GRID ON 命令在当前图形中 添加网格
GRID OFF 命令关闭当前图形网格
GRID 命令在网格状态间切换
>> grid on
7
在图形中增加其他曲线
HOLD ON 锁定当前的图形
HOLD OFF 释放当前图形
HOLD 在锁定状态间切换
>> addgraph
>> x = 0:.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> grid on
>> hold on
>> plot(x,exp(-x),'r:*')
8
控制视图区域在命令行,
ZOOM ON 打开视图区域选择观察功能
ZOOM OFF 关闭缩放功能
ZOOM 切换缩放操作状态
AXIS 设置轴坐标范围
[xmin xmax ymin ymax]
>> axis([0 2*pi 0 1])
ZOOM 使用图形窗口工具栏
9
图形标注
>> annotation
>> title('2-D Plots')
>> xlabel('Time')
>> ylabel('Sin(t)')
>> text(pi/3,sin(pi/3),...
'<--Sin(\pi/3)')
>> legend('Sine Wave',...
'Decaying Exponential')
10
特殊图形标注 (TeX)
上标和下标:
super and sub
字体:
Some boldface and some italics
希腊字符,
y = e?xsin(βx)
对 {}里面的注释内容有效,直至被修改或删除 ( \rm )
>> latex_examp
>> text(3.5,20,'y = e^{\alphax}sin(\betax)')
>> ylabel('Some \bfboldface\rm and some \it{italics}')
>> xlabel('^{super} and _{sub}')
参考,Special characters using LaTeX
11
>> curve_examp
%选择“旋转”按钮
%旋转所生成的曲面
%旋转是绘图编辑工
%具条上的选项之一绘图编辑工具栏
12
属性编辑器
图形编辑:
在窗口工具栏内点击图形对象编辑按扭
>> plotedit
当图形处于编辑状态时可以激活属性编辑器,
双击图形对象
鼠标右键点击目标并选择,Properties”选项
>> propedit
交互式绘图工具
>>plottool
13
属性编辑器 — Axis
14
删除 /拷贝当前对象
激活绘图编辑器并选择对象 (正弦波曲线 )
Ctrl-x - 剪切对象
Ctrl-c - 复制对象
Delete - 删除对象
15
练习,使用 2-D 绘图首先,装载数据。这是 10年内 Boston,MA和 Naples,FL每年的房价:
>> load reloc_data
在内存中,有一个变量名为 relo_data的 11x3 的矩正。
这个矩正有以下三列,
从 1990到至今的年份数
在 Boston,MA,有两间卧室房子的平均价格
在 Naples,FL,有两间卧室房子的平均价格使用 MATLAB的 2-D 绘图能力,绘制数据图并比较,
在同一个 figure窗口下绘制两个曲线图,每一个曲线图包括对应年份的房屋价格,
增加标题、图例以及坐标轴标签,
坐标轴标签使用 14号字,
16
解答:使用 2-D绘图
>> load reloc_data
>> whos
>> numyears=relo_data(:,1);
>> Bosprices=relo_data(:,2);
>> Naprices=relo_data(:,3);
>> plot(numyears,Bosprices)
>> hold on
>> plot(numyears,Naprices,'r:*')
>> title('Housing prices in Boston,MA versus Naples,FL')
>> xlabel('\fontsize{14}{Number of Years}')
>> ylabel('\fontsize{14}{Price Ranges}')
>> legend('Boston','Naples')
>> reloc_soln
17
保存和打开图形
>> open('plot_3d.fig')
18
把图形导出到文件部分支持的格式:
JPEG images (*.jpg)
Bitmap files (*.bmp)
TIFF images (*.tif)
EPS files (*.eps)
等等,…
19
从命令行导出图形用 PRINT命令生成硬拷贝输出,
语法,
仅用 PRINT命令把当前图形传送到当前打印机,
指定打印设备 (比如,以 TIFF或 PostScript 格式输出,或是控制输出到打印机内容的打印驱动设备 ):
>> print -device
>> print -device -options
指定一个文件名把输出直接输出到指定文件而不是打印机,
>> print -device -options filename
PRINTOPT 是一个 M文件,用户或系统管理员可以编辑它,并且可以确定缺省的打印机类型和目标文件,调用它时返回缺省值:
>> [pcmd,dev] = printopt >> help print
>> help printopt
20
拷贝到剪贴扳拷贝到剪贴板,
选项,
(Edit / Copy Options)
拷贝,
(Edit / Copy Figure)
0 1 2 3 4 5 6
0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
1
< - - S i n ( p / 3 )
2 - D P l o t s
S
i
n
(
t
)
T i m e
S i n e W a v e
D e c a y i n g E x p o n e n t i a l
21
拷贝选项
22
数据插值
INTERP1() - 1-D插值 (查表 )
INTERP2(),INTERP3(),INTERPN(),....
yi = interp1(x,y,xi,method)
>> interp_ex
23
求满足这些方程的斜率和截距
(y = mx + b)
示例:使用“左除”作一阶线性拟合
>> x = [2 1; 3 1; -3 1];
>> y = [12; 17; -5];
>> mb = x\y
mb =
3.5806 %Slope
5.6129 %Intercept
% Now evaluate y = mx + b
>> y_fit = mb(1)*x(:,1) + mb(2)
y_fit =
12.7742
16.3548
-5.1290
12 = m*2 + 1*b
17 = m*3 + 1*b
-5 = m*-3 + 1*b
>> solve_examp2
24
多项式拟合
POLYFIT - 多项式数据拟合
POLYVAL - 多项式计算
Left Division>> x = 0:.1:10;
>> y = sin(x)+cos(2*x);
>> k5 = polyfit(x,y,5);
>> y5 = polyval(k5,x);
>> k11 = polyfit(x,y,11);
>> y11 = polyval(k11,x);
>> plot(x,y,'g',...
x,y5,'r',...
x,y11,'b')
>> curve_fit
25
基本拟合工具
26
基本拟合工具,显示拟合点
27
练习:多项式拟合从载入数据开始,
>> load dessertdata
两个 1X10向量,温度和价格,将被载入到 workspace。 温度向量包含冰淇淋蛋筒每种价格下对应的温度。
使用基本拟合工具箱对数据进行四阶和五阶拟合
把系数值以结构体数组形式保存到工作区
也可以使用 POLYFIT 和 POLYVAL命令来完成这项工作。
附加题当使用更高阶次的多项式去拟合数据时发生了什么?
28
解答:多项式拟合
29
解答,在命令行进行多项式拟合
load dessertdata %载入甜点的绘图数据
plot(temps,prices)
hold on
k4 = polyfit(temps,prices,4); %四阶拟合
ft4 = polyval(k4,temps); %求解多项式
plot(temps,ft4,‘r’) %绘图
k5 = polyfit(temps,prices,5); %5阶拟合
ft5 = polyval(k5,temps); %求解多项式
plot(temps,ft5,‘k’) %绘图
xlabel('Temperature (degrees F)')
ylabel(‘Prices (cents)')
title(‘Price change for ice pops on a July day')
legend('Temperature Data',‘Fourth Order Fit',...
‘Fifth Order Fit',2)
>> trendfit_soln
30
解决方法,多项式拟合 (续 )
>> trendfit_soln
附加题当采用高于 6阶的式子拟合时会发生什么?
会得到警告信息当试图对一个很接近奇异 (不可逆 )的矩阵求逆时就会发生这样的情况,
这可能是由于使用阶数过高的多项式拟合数据,
想象一下用一个巨大的锤子去挂一幅油画时会发生些什么,
Warning,Polynomial is badly conditioned,Remove repeated data points
or try centering and scaling as described in HELP POLYFIT.
(Type "warning off MATLAB:polyfit:RepeatedPointsOrRescale" to
suppress this warning.)
> In C:\MATLAB6p5\toolbox\matlab\polyfun\polyfit.m at line 75
31
数据统计工具
>> load census
>> plot(cdate,pop,'*')
32
3-D 曲线绘图参考,Color,Linestyle,Marker options
>> z = 0:0.1:40;
>> x = cos(z);
>> y = sin(z);
>> plot3(x,y,z)
plot3(xdata,ydata,zdata,'clm',...)
>> plot_3d
33
3-D 曲面绘图
>> surf_3d
34
专用绘图语句
>> spec_plots
35
专用绘图语句 (续 )
>> spec_plots2
36
子图
SUBPLOT- 在同一图形窗口中显示多个图形
>> subplotex
subplot(#rows,#cols,index)
>> subplot(2,2,1);
>> plot(1:10)
>> subplot(2,2,2)
>> x = 0:.1:2*pi;
>> plot(x,sin(x))
>> subplot(2,2,3)
>> x = 0:.1:2*pi;
>> plot(x,exp(-x),'r')
>> subplot(2,2,4)
>> plot(peaks)
37
子图的使用
>>usingsubplots
38
不同的坐标轴刻度
SEMILOGY
Y轴线性
X轴对数
PLOTYY
2 组线性轴
LOGLOG
两轴均为对数刻度
SEMILOGX
X轴对数
Y轴线性
>> other_axes
39
图像 减少内存的占用量,
图像以 UINT8 - 1字节方式代表>> a = magic(4)
a =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> image(a);
>> map = hsv(16)
map =
1.0000 0 0
1.0000 0.3750 0
1.0000 0.7500 0
.....
>> colormap(map) >> imagex
用 colormap
第 2行数据画像素点 l (1,2)
第 2行
40
Colormap 编辑器
>> colormapeditor
当前 colormap
节点颜色设置,其它颜色用内插的方式得到双击节点,设定其颜色
41
示例,图像( images)和调色板( Colormaps)
>> draw_cape
>> load cape
>> image(X)
>> colormap(map)
MathWorks 公司
42
本章小结
2-D 绘图
图形标注
保存和导出图形
插值和曲线拟合
3-D 绘图
专用绘图指令
子图
坐标轴设置
图像和 colormap