天天看點

matlab畫圖(一)

例1、畫出函數圖像

>> x=-pi/2:0.01:pi/2;
>> y=x+sin(x)+exp(x);
>> plot(x,y,'r','Linewidth',2)
 
>> title('函數圖像');
>> xlabel('x軸');
>> ylabel('y軸')           

給圖像添加注釋:

title(‘y的函數圖像’) 

 xlabel(‘x軸注釋’) 

 ylabel(‘y軸的注釋’)

legend(‘y=x+sin(x)+e^x’):圖像示例

figure(n):表示第n個圖像

一、資料可視化——plot函數

(1)plot指令的一般使用規範

plot(x,y):以x為橫坐标,y為縱坐标,按照坐标(xj,yj)有序排列繪制圖像

(2)plot(y):y為一維數組,以1:n為橫坐标,yj為縱坐标,繪制圖線,n為y的長度

(3)plot(z)

其中z為一維複數數組,實數代表橫軸,虛數代表縱軸,在複平面上繪制(real(zj)),img(zj)的有序集合的圖形。

(4)plot(A)

A為矩陣,繪制矩陣A的列對它下标的圖形,矩陣A的行數表示橫坐标,該行上的元素表示縱坐标,例如

>> A=[1,2,3;2,3,4;5,6,7]

A =

     1     2     3
     2     3     4
     5     6     7

>> plot(A)
>> plot(A,'o')           

圖1

所繪制的曲線均采用顔色螢幕上不同的顔色繪制而成。

(5)plot(x,A)

A位矩陣,繪制A對向量X的圖形,對m*n的矩陣A和長度為m的向量x,繪制矩陣A的列對向量x的圖形;如果x的長度為n,則繪制矩陣A的行向量x的圖形,向量x可以是行向量,也可以是列向量。

(6)plot(A,x)

對矩陣A繪制向量x的圖形。對于一個m*n的矩陣A和一個長度為m的向量x,對矩陣A的列繪制向量x的圖形;如果x的長度為n,則對矩陣A 的行繪制向量x的圖形。向量x可以是行向量,也可以是列向量

(7)plot(A,B)

A,B為矩陣,對矩陣A的行繪制矩陣B的列圖形。如果A和B都是m*n的矩陣,則将會繪制h條由m個有序對煉成的曲線 ;

>> A

A =

     1     2     3
     2     3     4
     5     6     7

>> B

B =

     8     1     6
     3     5     7
     4     9     2

>> plot(A,B)           

(8)plot(x,y,str):指定str的顔色和線型繪制圖像

(9)plot(x1,y1,str,x2,y2,str2……)

二、添加格栅

grid函數對x,y軸增加格栅:grid on     grid  off  

set(gca,‘xgrid’  ,‘on’):函數功能是在x軸加格栅

set:設定目前句柄的屬性

on:打開

off:取消

ygrid:y軸的格栅

三、文字說明

①title(‘text’):在圖形視窗頂端的中間位置輸出字元串“text”作為标題

xlabel:

ylabel

zlabel:

②text(x,y,‘text’):在圖形視窗中的(x,y)處寫字元串“text”。坐标x,y按照所繪制的圖形的刻度給出。如果x和y是相同次元的向量,‘text’也是一個字元串向量,“text i”将會輸出在(xi ,yi)上

text(x,y,‘text’,‘sc’):左下角坐标為(0,0),右上角坐标為(1,1)。‘sc’表示将坐标抽歸一化!

③:legend(str1,str2,str3,……,pos)

目前圖像上輸出圖例,并說明字元串str1,str2等做标注。其中pos的可選項目如下,

pos取值:

-1 ====将圖例框放在坐标軸外的右側

0:====圖窗内與曲線交疊最小的位置

1====右上角

2===左上角

3===左下角

4===右下角

legend(str1,str2,str3,……,'location',pos):功能同上,其中參數pos的可選項如下:

north===圖内最上端

south===圖内最下端

east===圖内最右端

west===圖内最左端

northeast===右上角

northwest===左上角

………………………………………………一共18種

legend  off:從目前圖形中清除圖例

四、線型,顔色和标記

1、線型(5種)

-代表實作線

--代表虛線

-.點畫線

:點線

none:無線

2、标記

3、顔色

matlab平台中有代号的顔色有8種

g:綠色

m:品紅色

b:藍色

c:灰色

w:白色

r:紅色

k:黑色

y:黃色

五、坐标軸設定

matlab的坐标軸控制函數

axis auto:使用坐标軸的預設設定

axis  ([xmin,xmax,ymin,ymax]):分别設定x,y軸的坐标範圍

【注意:可以用xlim([xmin,xmax]),ylim([ymin,ymax])兩個函數對想軸和y軸單獨進行設定】

axis manual:保持目前坐标刻度範圍

--------------------------------------------------------------------------------------------------------------

axis off:取消坐标軸标簽,刻度及背景

axis on:打開坐标軸标簽、刻度及背景

-------------------------------------------------------------------------------------------------------------

axis fill:在manual方式下,有效,是坐标充滿整個繪圖區,

axis equal:橫,縱坐标采用等長刻度

axis image:橫縱坐标采用等長刻度,且坐标框緊貼資料範圍

axis tight:把資料範圍直接設定為坐标軸範圍

axis square:使用正方形坐标系

axis ij:使用矩陣坐标,遠點在左上方

axis normal:使用預設矩陣坐标系,取消機關刻度的限制

axis xy:使用直角坐标系,原點在左下方

------------------------------------------------------------------------------------------------------------

坐标軸的設定可以通過set函數進行設定:

例如:

set(gca,'XGrid','on')設定x軸上的格栅

set(gca,‘XColor’,‘red’)設定x軸顔色

 set(gca,'xlim',[xmin,xmax]):設定x軸的範圍

>> plot(x,y)
>> text(pi,0,'nizai')
>> legend('y=sin(x)',-1)
>> legend('y=sin(x)',1)
>> legend('y=sin(x)',2)
>> legend('y=sin(x)',3)
>> legend('y=sin(x)',0)
>> legend('y=sin(x)','Location',north)
Undefined function or variable 'north'.
 
>> legend('y=sin(x)','Location','north')
>> plot(x,y,'-.')
>> x=0:0.1:8*pi;
>> y1=sin(x);
>> y2=2*sin(x/2);
>> y3
Undefined function or variable 'y3'.
 
>> y3=4*sin(x/4);
>> plot(x,y1,'r*--',x,y2,'gsquare',x,y3,'g--')
>> lengend(' y1=sin(x)','y2=2*sin(x/2)','y3=4*sin(x/4)')
Undefined function 'lengend' for input arguments of type 'char'.
 
>> legend(' y1=sin(x)','y2=2*sin(x/2)','y3=4*sin(x/4)')
>> legend(' y1=sin(x)','y2=2*sin(x/2)','y3=4*sin(x/4)',0)
>> axis off
>> axis on
>> axis ij
>> axis normal
>> axis auto
>> axis xy
>> set(gca,'xlim[-10,10]')
Error using hg.axes/set
The name 'xlim[-10,10]' is not an accessible property for an instance of class
'axes'.
 
>> set(gca,'xlim'[-10,10])
 set(gca,'xlim'[-10,10])
              |
Error: Unbalanced or unexpected parenthesis or bracket.
 
>> set(gca,'XColor','red')
>> set(gca,'XLim'[-10,10])
 set(gca,'XLim'[-10,10])
              |
Error: Unbalanced or unexpected parenthesis or bracket.
 
>> set(gca,'XLim[-10,10]')
Error using hg.axes/set
The name 'XLim[-10,10]' is not an accessible property for an instance of class
'axes'.
 
>> set(gca,'XLim([-10,10])')
Error using hg.axes/set
The name 'XLim([-10,10])' is not an accessible property for an instance of class
'axes'.
 
>> help(xlim)%help函數中的參數為字元串
Error using help (line 71)
Argument to help must be a string.
 
>> help(XLim)
Cannot find an exact (case-sensitive) match for 'XLim'

The closest match is: xlim
in D:\MATLAB\R2012a\toolbox\matlab\graph3d\xlim.m

 
>> help(xlim)
Error using help (line 71)
Argument to help must be a string.
 
>> help('xlim')
 xlim X limits.
    XL = xlim             gets the x limits of the current axes.
    xlim([XMIN XMAX])     sets the x limits.
    XLMODE = xlim('mode') gets the x limits mode.
    xlim(mode)            sets the x limits mode.
                             (mode can be 'auto' or 'manual')
    xlim(AX,...)          uses axes AX instead of current axes.
 
    xlim sets or gets the XLim or XLimMode property of an axes.
 
    See also pbaspect, daspect, ylim, zlim.

    Reference page in Help browser
       doc xlim

>> set(gca,'xlim'[-10,10])
 set(gca,'xlim'[-10,10])
              |
Error: Unbalanced or unexpected parenthesis or bracket.
 
>> set(gca,'xlim([-10,10])')
Error using hg.axes/set
The name 'xlim([-10,10])' is not an accessible property for an instance of class
'axes'.
 
>> set(gca,'xlim'([-10,10]))
 set(gca,'xlim'([-10,10]))
              |
Error: Unbalanced or unexpected parenthesis or bracket.
 
>> set(gca,'xlim',[-10,10])
>> set(gca,'xlim',[0,30])
>> set(gca,'xlim',[0,30])           

六、圖像疊繪

hold on 語句:語句功能為使目前軸及圖形保留下來而不被重新整理,并接受即将繪制的新的曲線

hold  off:語句功能為不保留目前軸及圖形,繪制新的曲線之後,原圖被重新整理。

hold語句:為hold on  和hold off 語句的切換

繼續閱讀