【转载】 Matlab中的画图函数(可视化编程)

   
    之前在进行Matlab编程时,画图总是非常重要的一部分,在这里整理一下常用的绘图函数,以作备用。



一、二维曲线和图形

    MATLAB提供了多种二维图形的绘制命令

命令名含义和功能
area面域图;主要用于表现比例、成分
bar直方图;主要用于统计数据
compass射线图;主要用于方向和速度
feather羽毛图;主要用于速度
hist频数直方图;主要用于统计
pie二维饼图;统计数据极坐标形式
plot基本二维曲线图形指令
polar以极坐标绘制曲线
quiver二维箭头图;主要用于场强、流向
rose频数扇形图;主要用于统计
stairs阶梯图;主要用于采样数据
stem二维杆图;主要用于离散数据

1、二维图像基本命令plot

(1)、基本调用格式plot(x,y,’s’)

    这是plot命令最经典、最基本的调用格式。该命令的输入量(x,y,’s’)称为平面绘图三元组。他们分别指定平面曲线的几何位置、点形、线型和色彩。

1.曲线线型、颜色和标记点类型
plot(X1,Y1,LineSpec, …) 通过字符串LineSpec指定曲线的线型、颜色及数据点的标记类型。

符号意义
-实线
r红色
+加号
-.点划线
g绿色
o圆圈
虚线
b蓝色
*星号
点线
c蓝绿色
.
m洋红色
x交叉符号
y黄色
s方格
k黑色
d菱形
w白色
^向上的三角形
>向左的三角形

<       向右的三角形 
p五边形
h六边形
  
  
  

 

2.设置曲线线宽、标记点大小,标记点边框颜色和标记点填充颜色等。
plot(…,’Property Name’, Property Value, …)

Property Name 意义    选项

LineWidth 线宽    数值,如0.5,1等,单位为points
MarkerEdgeColor 标记点边框线条颜色 颜色字符,如’g’, ’b’等
MarkerFaceColor 标记点内部区域填充颜色 颜色字符
MarkerSize 标记点大小   数值,单位为points

3.坐标轴设置
范围设置:
a. axis([xmin xmax ymin ymax])设置坐标轴在指定的区间
b. axis auto 将当前绘图区的坐标轴范围设置为MATLAB自动调整的区间
c. axis manual 冻结当前坐标轴范围,以后叠加绘图都在当前坐标轴范围内显示
d. axis tight 采用紧密模式设置当前坐标轴范围,即一用户数据范围为坐标轴范围
比例:
a. axis equal 等比例坐标轴
b. axis square 以当前坐标轴范围为基础,将坐标轴区域调整为方格形
c. axis normal 自动调整纵横轴比例,使当前坐标轴范围内的图形显示达到最佳效果
范围选项和比例设置可以联合使用,默认的设置为axis auto normal

4.坐标轴刻度设置
set(gca, ’XTick’, [0 1 2]) X坐标轴刻度数据点位置
set(gca,’XTickLabel’,{‘a’,’b’,’c’}) X坐标轴刻度处显示的字符
set(gca,’FontName’,’Times New Roman’,’FontSize’,14)设置坐标轴刻度字体名称,大小
‘FontWeight’,’bold’ 加粗 ‘FontAngle’,’italic’ 斜体
对字体的设置也可以用在title, xlabel, ylabel等中

5.图例
legend(‘a’,’Location’,’best’) 图例位置放在最佳位置

6.更多的设置可以在绘图窗口中打开绘图工具,Inspector… 中查找

http://hi.baidu.com/monkeyking_fu/blog/item/5e9c65160654b956f2de325a.html

Various line types, plot symbols and colors may be obtained with
PLOT(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:

         b     blue       .     point              -     solid
         g     green       o     circle          :     dotted
         r     red           x     x-mark          -. dashdot
         c     cyan       +     plus             – dashed
         m     magenta    *     star          (none)   no line
         y     yellow        s     square
         k     black       d     diamond
         w     white       v     triangle (down)
                          ^     triangle (up)
                          <     triangle (left)
                          >     triangle (right)
                          p     pentagram
                          h     hexagram

http://www.ilovematlab.cn/thread-12702-1-1.html

在使用Matlab时,经常需要将得到的数值表达成二维或三维图像。

plot(vector1,vector2)可以用来画两个矢量的二维图,例如

x=1:0.1:2*pi;

plot(x,sin(x))可以画正弦函数在0-2pi的上的图像。

plot函数可以接一些参数,来改变所画图像的属性(颜色,图像元素等)。下面是一些属性的说明
          b     blue(蓝色)       .     point(点)       -     solid(实线)
          g     green(绿色)      o     circle(圆圈)    :     dotted(点线)
          r     red(红色)        x     x-mark(叉号)    -.    dashdot (点画线)
          c     cyan(墨绿色)     +     plus(加号)       –    dashed(虚线)
          m     magenta(紫红色) *     star(星号)      (none) no line
          y     yellow(黄色)     s     square(正方形)
          k     black(黑色)      d     diamond(菱形)
                              v     triangle (down)
                              ^     triangle (up)
                              <     triangle (left)
                              >     triangle (right)
                              p     pentagram
                              h     hexagram
例如,plot(x,y,’.r’)表示用点来画图,点的颜色是红色。

http://gdcxb2007.blogbus.com/logs/46319055.html

plot函数可以接一些参数,来改变所画图像的属性(颜色,图像元素等)。下面是一些属性的说明

          b     blue(蓝色)       .     point(点)       -     solid(实线)

          g     green(绿色)      o     circle(圆圈)    :     dotted(点线)

          r     red(红色)        x     x-mark(叉号)    -.    dashdot (点画线)

          c     cyan(墨绿色)     +     plus(加号)       –    dashed(虚线)

          m     magenta(紫红色) *     star(星号)      (none) no line

          y     yellow(黄色)     s     square(正方形)

          k     black(黑色)      d     diamond(菱形)

                              v     triangle (down)

                              ^     triangle (up)

                              <     triangle (left)

                              >     triangle (right)

                              p     pentagram

                              h     hexagram

 

   Example

      x = -pi:pi/10:pi;

      y = tan(sin(x)) - sin(tan(x));

      plot(x,y,’–rs’,’LineWidth’,2,…

                      ‘MarkerEdgeColor’,’k’,…

                      ‘MarkerFaceColor’,’g’,…

                      ‘MarkerSize’,10)

          xlabel(‘x’);

          ylabel(‘y’);

·         用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的运动情况。

legend函数的基本用法是

LEGEND(string1,string2,string3, …)

分别将字符串1、字符串2、字符串3……标注到图中,每个字符串对应的图标为画图时的图标。

例如:

plot(x,sin(x),’.b’,x,cos(x),’+r’)

legend(‘sin’,’cos’)这样可以把”.”标识为’sin’,把”+”标识为”cos”

还可以用LEGEND(…,’Location’,LOC) 来指定图例标识框的位置

这些是Matlab help文件。后面一段是对应的翻译和说明

       ‘North’              inside plot box near top

       ‘South’              inside bottom

       ‘East’               inside right

       ‘West’               inside left

       ‘NorthEast’          inside top right (default)

       ‘NorthWest           inside top left

       ‘SouthEast’          inside bottom right

       ‘SouthWest’          inside bottom left

       ‘NorthOutside’       outside plot box near top

       ‘SouthOutside’       outside bottom

       ‘EastOutside’        outside right

       ‘WestOutside’        outside left

       ‘NorthEastOutside’   outside top right

       ‘NorthWestOutside’   outside top left

       ‘SouthEastOutside’   outside bottom right

       ‘SouthWestOutside’   outside bottom left

       ‘Best’               least conflict with data in plot

       ‘BestOutside’        least unused space outside plot

       ‘North’             图例标识放在图顶端

       ‘South’            图例标识放在图底端

       ‘East’               图例标识放在图右方

       ‘West’              图例标识放在图左方

       ‘NorthEast’       图例标识放在图右上方(默认)

       ‘NorthWest      图例标识放在图左上方

       ‘SouthEast’      图例标识放在图右下角

       ‘SouthWest’     图例标识放在图左下角

(以上几个都是将图例标识放在框图内)

       ‘NorthOutside’          图例标识放在图框外侧上方

       ‘SouthOutside’         图例标识放在图框外侧下方

       ‘EastOutside’           图例标识放在图框外侧右方

       ‘WestOutside’          图例标识放在图框外侧左方

       ‘NorthEastOutside’   图例标识放在图框外侧右上方

       ‘NorthWestOutside’ 图例标识放在图框外侧左上方

       ‘SouthEastOutside’   图例标识放在图框外侧右下方

       ‘SouthWestOutside’ 图例标识放在图框外侧左下方

(以上几个将图例标识放在框图外)

       ‘Best’                      图标标识放在图框内不与图冲突的最佳位置

       ‘BestOutside’           图标标识放在图框外使用最小空间的最佳位置

还是用上面的例子

legend(‘sin’,’cos’,’location’,’northwest’)可以将标识框放置在图的左上角。

   Examples:

       x = 0:.2:12;

       plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));

       legend(‘First’,’Second’,’Third’);

       legend(‘First’,’Second’,’Third’,’Location’,’NorthEastOutside’)

       b = bar(rand(10,5),’stacked’); colormap(summer); hold on

       x = plot(1:10,5*rand(10,1),’marker’,’square’,’markersize’,12,…

                ‘markeredgecolor’,’y’,’markerfacecolor’,[.6 0 .6],…

                ‘linestyle’,’-‘,’color’,’r’,’linewidth’,2); hold off

       legend([b,x],’Carrots’,’Peas’,’Peppers’,’Green Beans’,…

                 ‘Cucumbers’,’Eggplant’)


图形的控制与表现 (Figure control and representation)

MATLAB提供的用于图形控制的函数和命令:
   axis: 人工选择坐标轴尺寸.
    clf:清图形窗口.
 ginput: 利用鼠标的十字准线输入.
   hold: 保持图形.
    shg:显示图形窗口.
subplot: 将图形窗口分成N块子窗口。

1.图形窗口(figure window)
(1). 图形窗口的创建和选择(Creating and selecting of figure window)
     figure(n):用于为当前的绘图创建图形窗口,每运行一次figure就会创建一个新的图形窗口,n表示第n个

                窗口,如果窗口定义了句柄,也可以用figure(h)将句柄h的窗口作为当前窗口。
          clf :用于清除当前图形窗口中的内容。
          shg :用于显示当前图形窗口。


(2). 在一个图形窗口中绘制多个子图形(Drawing several subfigures in a single window)
subplot(m,n,p):把窗口分成m×n个小窗口,并把第p个窗口当作当前窗口。
例:将4 个图形显示在同一个图形窗口中。
    t=0:pi/20:2*pi; [x,y]=meshgrid(t);
    subplot(2,2,1); plot(sin(t),cos(t)); axis equal
    subplot(2,2,2); z=sin(x)+cos(y); plot(t,z); axis([0 2*pi –2 2])
    subplot(2,2,3); z=sin(x).*cos(y); plot(t,z); axis([0 2*pi –1 1])
    subplot(2,2,4); z=sin(x).^2-cos(y).^2; plot(t,z); axis([0 2*pi –1 1])


(3). 在一个已有的图形上绘图(Drawing a figure on the figure was existed)
      hold on :在一个已有的图形上继续绘图;

      hold off: 命令结束继续绘图。
例:将peaks函数的等高线图与伪彩色画在一起。
    [x,y,z]=peaks;          %产生双变量数组
    contour(x,y,z,20,’k’)        %绘制等高线
    hold on
    pcolor(x,y,z)             %绘制伪彩色图
    shading interp          %表面色彩渲染
    hold off


2.坐标轴控制命令(Axis control commands)
   控制坐标性质的axis函数的多种调用格式:
axis(xmin xmax ymin ymax) :指定二维图形x和y轴的刻度范围,
axis auto                  :设置坐标轴为自动刻度(缺省值)
axis manual(或axis(axis)):保持刻度不随数据的大小而变化
axis tight                 :以数据的大小为坐标轴的范围
axis ij                    :设置坐标轴的原点在左上角,i为纵坐标,j为横坐标
axis xy                    :使坐标轴回到直角坐标系
axis equal                 :使坐标轴刻度增量相同
axis square                :使各坐标轴长度相同,但刻度增量未必相同
axis normal                :自动调节轴与数据的外表比例,使其他设置失效
axis off                   :使坐标轴消隐
axis on                    :显现坐标轴


(1) 坐标轴的范围(Domain of coordinates axis)
二维图形坐标轴范围在缺省状态下是根据数据的大小自动设置的,如欲改变,可利用axis(xmin xmax ymin ymax),函数来定义。
例: 定义坐标轴范围对观察图形的影响。
x=0:.01:pi/2; figure(1); plot(x,tan(x),’-ro’)     %ymax=tan(1.57),而其他数据都很小,结果将
%使图形难于进行观察和判断。
figure(2); plot(x,tan(x),’-ro’); axis([0, pi/2,0,5])   %对坐标轴的范围进行控制就可得到较满意的绘图结果


(2) 显示比例对绘图结果的影响(Effect of display scaling on plotting results)
例:比较(Default, axis square, axis equal, axis tight)几种不同的显示方式的显示效果。
t=0:pi/20:2*pi; figure(1);
subplot(2,1,1); plot(sin(t),2*cos(t)); grid on     %缺省状态下的图形比例
subplot(2,1,2); plot(sin(t),2*cos(t)); axis square; grid on    %正方形的显示比例
figure(2)
subplot(1,2,1); plot(sin(t),2*cos(t)) ; axis equal; grid on           %具有相等的刻度比例
subplot(1,2,2); plot(sin(t),2*cos(t)); axis tight ; grid on              %紧缩形式

3.图形标注(Marking on the figure):MATLAB的图形标注方法(表 6—7)
 title :标题,
xlabel :x轴标注,
ylabel :y轴标注,
  text :任意定位的标注                      
 gtext :鼠标定位标注,
legent :标注图例


图形标注可以使用字母,数字,汉字或按规定的方法表示希腊字母。如:pi表示π,leq表示≤,rm表示后面的字恢复为正体字,it表示斜体字,FontSize表示字体的大小, FontName表示字体的类型等。
可以使用图形窗口的Insert菜单,也可以使用属性编辑器,还可以使用函数输入的方法加标注,以下介绍相关函数的使用方法。


(1). 加注坐标轴标识和图形标题(Add axis labels and title of figure)
加注坐标轴标识:xlabel(‘s’), ylabel(‘s’)
      图形标题: title(‘s’)
例:加注坐标轴标示和图形标题。
    t=0:pi/100:2*pi;y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])
    xlabel(‘0 leq itt rm leq pi’,’FontSize’,16)
    ylabel(‘sin(t)’,’FontSize’,20)
    title(‘正弦函数图形’,’FontName’,’隶书’,’FontSize’,20)


(2). 图中加注文本(Add text in the figure)
text(x,y,’字符串’)
例:在上图中加语句。
    t=0:pi/100:2*pi;
    y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])                               
    xlabel(‘0 leq itt rm leq pi’,’FontSize’,16)
    ylabel(‘sin(t)’,’FontSize’,20)
    title(‘正弦函数图形’,’FontName’,’隶书’,’FontSize’,20)
    text(3*pi/4,sin(3*pi/4),’leftarrowsin(t)=0.707’, ‘FontSize’,16)
    text(pi,sin(pi),’leftarrowsin(t)=0’, ‘FontSize’,16)
    text(5*pi/4,sin(5*pi/4),’sin(t)=-0.707rightarrow’,’FontSize’,16,…

         ‘HorizontalAlignment’,’right’)
句中:
leftarrow           表示加一个向左的箭头
rightarrow          表示加一个向右的箭头
HorizontalAlignment 表示右对齐水平排列


gtext(‘字符串’): 在图形窗口上用鼠标直接在指定的位置上加注文本。

例:
    t=0:pi/100:2*pi;
    y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])
    xlabel(‘0 leq itt rm leq pi’,’FontSize’,16)
    ylabel(‘sin(t)’,’FontSize’,20)
    title(‘正弦函数图形’,’FontName’,’隶书’,’FontSize’,20)
    gtext(‘MATLAB’)


(3). 指定TeX字符
例:在标题中指定TeX字符
    t=0:pi/100:2*pi;
    alpha=-0.8;
    beta=15;
    y=sin(beta*t).*exp(alpha*t);
    plot(t,y)
    title(‘{itAe}^{-italphaitt}sinitbeta{itt}italpha<<itbeta’)
    xlabel(‘时间mus.’),
    ylabel(‘幅值’)
在title中的字符串表现的是 Aeαt   sinβt   α<<β
斜体Ae 上标斜体αt 斜体βt 斜体α  斜体β


(4). 在图形中添加图例框(Add legend in the figure)
legend(字符串1,字符串2,…)
例:在当前图形中添加图例说明。
    x=0:pi/10:2*pi;
    y1=sin(x);
    y2=0.6*sin(x);
    y3=0.3*sin(x);
    plot(x,y1,x,y2,’-o’,x,y3,’-*’)
    legend( ‘曲线1’,’曲线2’,’曲线3’)
    legend(‘boxoff’)
legend函数的其他功能见(表 6—8)

4. 图线形式和颜色(Style and color of plot)
(1). 图线的形式: (style of plot)

  四种线形: 实线’-‘,虚线’–’, 点线’:’,点划线’-‘.
标记点类型:点’.’, 圆’o’, 加号’+’, 星号’*’, x符号’x’, 方形’s’, 菱形’d’, hexagram ‘h’

            上三角△ ‘^’, 下三角▽ ‘v’, 左三角’<’, 右三角’>’, 正五边形’p’

 

命令:plot(x,y,’—’), plot(x1,y1,’:’,x2,y2,’*’)
例1:选择不同的线形绘图。    
    t=0:pi/100:2*pi; y=sin(t); y2=sin(t-0.25); y3=sin(t-0.5);
    plot(t,y,’-‘,t,y2,’-‘,t,y3,’:’)
例2:选择不同的标记点绘图。
   t=0:pi/20:2*pi; x=t.^3; y=sin(t); plot(x,y,’o’)


(2). 线的颜色(color of plot)

可选颜色: 红r,绿g, 蓝b, 黄y, 粉红m, 青c, 黑k.
例:t=0:pi/20:2*pi;
    y=sin(t); plot(x,y,’r’), plot(x,y,’g+’)


(3). 图线的其他属性(other characters of plot)

设置图线的宽度 : ‘LineWidth’

标记点的边缘颜色: ‘MarkerEdgeColor’

填充颜色        : ‘MarkerFaceColor’

标记点的大小    : ‘MarkerSize’

例: 设置图线的线形、颜色、宽度、标记点的颜色及大小。
    t=0:pi/20:pi; y=sin(4*t).*sin(t)/2;
    plot(t,y,’-bs’,’LineWidth’,2,’MarkerEdgeColor’,’k’, ‘MarkerFaceColor’, ‘y’,’MarkerSize’,10);


            </div>
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值