matlab命令及海洋作图
常用命令
addpath(genpath(‘文件夾路徑’)) 將某個(gè)文件夾及其子路徑加入搜索路徑
rmpath(path) 刪除某一路徑
save path 將添加和刪除的路徑保存
squeeze(A) 刪除單一維度
m_map工具箱
m_map工具箱的應(yīng)用,參考:
https://www.eoas.ubc.ca/~rich/map.html
主要用于地圖投影作圖,比起matlab自帶的contourfm等函數(shù),bug和問題相對(duì)較少
matlab作矢量圖
m_quiver畫海流、風(fēng)場(chǎng)等矢量時(shí),不好畫refrence vector
推薦使用m_vec函數(shù),reference vector的畫法基本就是在空白處再畫一個(gè)箭頭標(biāo)上數(shù)字和單位即可,m_vec腳本開頭有一個(gè)詳細(xì)的實(shí)例可以參考。
對(duì)于模式輸出矢量如果太密,采用的方式是等間隔取格點(diǎn)來作圖
基本繪圖命令
1.曲線線型、顏色和標(biāo)記點(diǎn)類型
plot(X1,Y1,LineSpec, …) 通過字符串LineSpec指定曲線的線型、顏色及數(shù)據(jù)點(diǎn)的標(biāo)記類型。
plot函數(shù)可以接一些參數(shù),來改變所畫圖像的屬性(顏色,圖像元素等)。下面是一些屬性的說明
b blue(藍(lán)色) . point(點(diǎn)) - solid(實(shí)線)
g green(綠色) o circle(圓圈) : dotted(點(diǎn)線)
r red(紅色) x x-mark(叉號(hào)) -. dashdot (點(diǎn)畫線)
c cyan(墨綠色) + plus(加號(hào)) – dashed(虛線)
m magenta(紫紅色) * star(星號(hào)) (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’)表示用點(diǎn)來畫圖,點(diǎn)的顏色是紅色。
2.設(shè)置曲線線寬、標(biāo)記點(diǎn)大小,標(biāo)記點(diǎn)邊框顏色和標(biāo)記點(diǎn)填充顏色等。
plot(…,’Property Name’, Property Value, …)
Property Name 意義 選項(xiàng)
LineWidth 線寬 數(shù)值,如0.5,1等,單位為points
MarkerEdgeColor 標(biāo)記點(diǎn)邊框線條顏色 顏色字符,如’g’, ’b’等
MarkerFaceColor 標(biāo)記點(diǎn)內(nèi)部區(qū)域填充顏色 顏色字符
MarkerSize 標(biāo)記點(diǎn)大小 數(shù)值,單位為points
3.坐標(biāo)軸設(shè)置
范圍設(shè)置:
a. axis([xmin xmax ymin ymax])設(shè)置坐標(biāo)軸在指定的區(qū)間
b. axis auto 將當(dāng)前繪圖區(qū)的坐標(biāo)軸范圍設(shè)置為MATLAB自動(dòng)調(diào)整的區(qū)間
c. axis manual 凍結(jié)當(dāng)前坐標(biāo)軸范圍,以后疊加繪圖都在當(dāng)前坐標(biāo)軸范圍內(nèi)顯示
d. axis tight 采用緊密模式設(shè)置當(dāng)前坐標(biāo)軸范圍,即一用戶數(shù)據(jù)范圍為坐標(biāo)軸范圍
比例:
a. axis equal 等比例坐標(biāo)軸
b. axis square 以當(dāng)前坐標(biāo)軸范圍為基礎(chǔ),將坐標(biāo)軸區(qū)域調(diào)整為方格形
c. axis normal 自動(dòng)調(diào)整縱橫軸比例,使當(dāng)前坐標(biāo)軸范圍內(nèi)的圖形顯示達(dá)到最佳效果
范圍選項(xiàng)和比例設(shè)置可以聯(lián)合使用,默認(rèn)的設(shè)置為axis auto normal
4.坐標(biāo)軸刻度設(shè)置
set(gca, ’XTick’, [0 1 2]) X坐標(biāo)軸刻度數(shù)據(jù)點(diǎn)位置
set(gca,‘XTickLabel’,{‘a(chǎn)’,‘b’,‘c’}) X坐標(biāo)軸刻度處顯示的字符
set(gca,‘FontName’,‘Times New Roman’,‘FontSize’,14)設(shè)置坐標(biāo)軸刻度字體名稱,大小
‘FontWeight’,’bold’ 加粗 ‘FontAngle’,’italic’ 斜體
對(duì)字體的設(shè)置也可以用在title, xlabel, ylabel等中
5.圖例
legend(‘a(chǎn)’,‘Location’,‘best’) 圖例位置放在最佳位置
legend函數(shù)的基本用法是
LEGEND(string1,string2,string3, …)
分別將字符串1、字符串2、字符串3……標(biāo)注到圖中,每個(gè)字符串對(duì)應(yīng)的圖標(biāo)為畫圖時(shí)的圖標(biāo)。
例如:
plot(x,sin(x),’.b’,x,cos(x),’+r’)
legend(‘sin’,‘cos’)這樣可以把".“標(biāo)識(shí)為’sin’,把”+“標(biāo)識(shí)為"cos”
還可以用LEGEND(…,‘Location’,LOC) 來指定圖例標(biāo)識(shí)框的位置
'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' 圖例標(biāo)識(shí)放在圖頂端'South' 圖例標(biāo)識(shí)放在圖底端'East' 圖例標(biāo)識(shí)放在圖右方'West' 圖例標(biāo)識(shí)放在圖左方'NorthEast' 圖例標(biāo)識(shí)放在圖右上方(默認(rèn))'NorthWest 圖例標(biāo)識(shí)放在圖左上方'SouthEast' 圖例標(biāo)識(shí)放在圖右下角'SouthWest' 圖例標(biāo)識(shí)放在圖左下角(以上幾個(gè)都是將圖例標(biāo)識(shí)放在框圖內(nèi))
'NorthOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)上方'SouthOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)下方'EastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右方'WestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左方'NorthEastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右上方'NorthWestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左上方'SouthEastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右下方'SouthWestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左下方(以上幾個(gè)將圖例標(biāo)識(shí)放在框圖外)
'Best' 圖標(biāo)標(biāo)識(shí)放在圖框內(nèi)不與圖沖突的最佳位置'BestOutside' 圖標(biāo)標(biāo)識(shí)放在圖框外使用最小空間的最佳位置legend(‘sin’,‘cos’,‘location’,‘northwest’)可以將標(biāo)識(shí)框放置在圖的左上角。
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 onx = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...'markeredgecolor','y','markerfacecolor',[.6 0 .6],...'linestyle','-','color','r','linewidth',2); hold offlegend([b,x],'Carrots','Peas','Peppers','Green Beans',...'Cucumbers','Eggplant')圖形的控制與表現(xiàn)
MATLAB提供的用于圖形控制的函數(shù)和命令:
axis: 人工選擇坐標(biāo)軸尺寸.
clf:清圖形窗口.
ginput: 利用鼠標(biāo)的十字準(zhǔn)線輸入.
hold: 保持圖形.
shg:顯示圖形窗口.
subplot: 將圖形窗口分成N塊子窗口。
1.圖形窗口(figure window)
(1). 圖形窗口的創(chuàng)建和選擇(Creating and selecting of figure window)
figure(n):用于為當(dāng)前的繪圖創(chuàng)建圖形窗口,每運(yùn)行一次figure就會(huì)創(chuàng)建 一個(gè)新的圖形窗口,n表示第n個(gè)窗口,如果窗口定義了句柄,也可以用figure(h)將句柄h的窗口作為當(dāng)前窗口。
clf :用于清除當(dāng)前圖形窗口中的內(nèi)容。
shg :用于顯示當(dāng)前圖形窗口。
(2). 在一個(gè)圖形窗口中繪制多個(gè)子圖形(Drawing several subfigures in a single window)
subplot(m,n,p):把窗口分成m×n個(gè)小窗口,并把第p個(gè)窗口當(dāng)作當(dāng)前窗口。
例:將4 個(gè)圖形顯示在同一個(gè)圖形窗口中。
t=0:pi/20:2pi; [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 2pi –2 2])
subplot(2,2,3); z=sin(x).cos(y); plot(t,z); axis([0 2pi –1 1])
subplot(2,2,4); z=sin(x).2-cos(y).2; plot(t,z); axis([0 2*pi –1 1])
(3). 在一個(gè)已有的圖形上繪圖(Drawing a figure on the figure was existed)
hold on :在一個(gè)已有的圖形上繼續(xù)繪圖;
例:將peaks函數(shù)的等高線圖與偽彩色畫在一起。
[x,y,z]=peaks; %產(chǎn)生雙變量數(shù)組
contour(x,y,z,20,‘k’) %繪制等高線
hold on
pcolor(x,y,z) %繪制偽彩色圖
shading interp %表面色彩渲染
hold off
2.坐標(biāo)軸控制命令(Axis control commands)
控制坐標(biāo)性質(zhì)的axis函數(shù)的多種調(diào)用格式:
axis(xmin xmax ymin ymax) :指定二維圖形x和y軸的刻度范圍,
axis auto :設(shè)置坐標(biāo)軸為自動(dòng)刻度(缺省值)
axis manual(或axis(axis)):保持刻度不隨數(shù)據(jù)的大小而變化
axis tight :以數(shù)據(jù)的大小為坐標(biāo)軸的范圍
axis ij :設(shè)置坐標(biāo)軸的原點(diǎn)在左上角,i為縱坐標(biāo),j為橫坐標(biāo)
axis xy :使坐標(biāo)軸回到直角坐標(biāo)系
axis equal :使坐標(biāo)軸刻度增量相同
axis square :使各坐標(biāo)軸長(zhǎng)度相同,但刻度增量未必相同
axis normal :自動(dòng)調(diào)節(jié)軸與數(shù)據(jù)的外表比例,使其他設(shè)置失效
axis off :使坐標(biāo)軸消隱
axis on :顯現(xiàn)坐標(biāo)軸
(1) 坐標(biāo)軸的范圍(Domain of coordinates axis)
二維圖形坐標(biāo)軸范圍在缺省狀態(tài)下是根據(jù)數(shù)據(jù)的大小自動(dòng)設(shè)置的,如欲改變,可利用axis(xmin xmax ymin ymax),函數(shù)來定義。
例: 定義坐標(biāo)軸范圍對(duì)觀察圖形的影響。
x=0:.01:pi/2; figure(1); plot(x,tan(x),’-ro’) %ymax=tan(1.57),而其他數(shù)據(jù)都很小,結(jié)果將
%使圖形難于進(jìn)行觀察和判斷。
figure(2); plot(x,tan(x),’-ro’); axis([0, pi/2,0,5]) %對(duì)坐標(biāo)軸的范圍進(jìn)行控制就可得到較滿意的繪圖結(jié)果
(2) 顯示比例對(duì)繪圖結(jié)果的影響(Effect of display scaling on plotting results)
例:比較(Default, axis square, axis equal, axis tight)幾種不同的顯示方式的顯示效果。
t=0:pi/20:2pi; figure(1);
subplot(2,1,1); plot(sin(t),2cos(t)); grid on %缺省狀態(tài)下的圖形比例
subplot(2,1,2); plot(sin(t),2cos(t)); axis square; grid on %正方形的顯示比例
figure(2)
subplot(1,2,1); plot(sin(t),2cos(t)) ; axis equal; grid on %具有相等的刻度比例
subplot(1,2,2); plot(sin(t),2*cos(t)); axis tight ; grid on %緊縮形式
3.圖形標(biāo)注(Marking on the figure):MATLAB的圖形標(biāo)注方法(表 6—7)
title :標(biāo)題,
xlabel :x軸標(biāo)注,
ylabel :y軸標(biāo)注,
text :任意定位的標(biāo)注
gtext :鼠標(biāo)定位標(biāo)注,
legent :標(biāo)注圖例
圖形標(biāo)注可以使用字母,數(shù)字,漢字或按規(guī)定的方法表示希臘字母。如:pi表示π,leq表示≤,rm表示后面的字恢復(fù)為正體字,it表示斜體字,FontSize表示字體的大小, FontName表示字體的類型等。
可以使用圖形窗口的Insert菜單,也可以使用屬性編輯器,還可以使用函數(shù)輸入的方法加標(biāo)注,以下介紹相關(guān)函數(shù)的使用方法。
(1). 加注坐標(biāo)軸標(biāo)識(shí)和圖形標(biāo)題(Add axis labels and title of figure)
加注坐標(biāo)軸標(biāo)識(shí):xlabel(‘s’), ylabel(‘s’)
圖形標(biāo)題: title(‘s’)
例:加注坐標(biāo)軸標(biāo)示和圖形標(biāo)題。
t=0:pi/100:2pi;y=sin(t);
plot(t,y)
axis([0 2pi,-1 1])
xlabel(‘0 leq itt rm leq pi’,‘FontSize’,16)
ylabel(‘sin(t)’,‘FontSize’,20)
title(‘正弦函數(shù)圖形’,‘FontName’,‘隸書’,‘FontSize’,20)
(2). 圖中加注文本(Add text in the figure)
text(x,y,’字符串’)
例:在上圖中加語(yǔ)句。
t=0:pi/100:2pi;
y=sin(t);
plot(t,y)
axis([0 2pi,-1 1])
xlabel(‘0 leq itt rm leq pi’,‘FontSize’,16)
ylabel(‘sin(t)’,‘FontSize’,20)
title(‘正弦函數(shù)圖形’,‘FontName’,‘隸書’,‘FontSize’,20)
text(3pi/4,sin(3pi/4),‘leftarrowsin(t)=0.707’, ‘FontSize’,16)
text(pi,sin(pi),‘leftarrowsin(t)=0’, ‘FontSize’,16)
text(5pi/4,sin(5pi/4),‘sin(t)=-0.707rightarrow’,‘FontSize’,16,…
句中:
leftarrow 表示加一個(gè)向左的箭頭
rightarrow 表示加一個(gè)向右的箭頭
HorizontalAlignment 表示右對(duì)齊水平排列
gtext(‘字符串’): 在圖形窗口上用鼠標(biāo)直接在指定的位置上加注文本。
例:
t=0:pi/100:2pi;
y=sin(t);
plot(t,y)
axis([0 2pi,-1 1])
xlabel(‘0 leq itt rm leq pi’,‘FontSize’,16)
ylabel(‘sin(t)’,‘FontSize’,20)
title(‘正弦函數(shù)圖形’,‘FontName’,‘隸書’,‘FontSize’,20)
gtext(‘MATLAB’)
(3). 指定TeX字符
例:在標(biāo)題中指定TeX字符
t=0:pi/100:2pi;
alpha=-0.8;
beta=15;
y=sin(betat).exp(alphat);
plot(t,y)
title(’{itAe}^{-italphaitt}sinitbeta{itt}italpha<<itbeta’)
xlabel(‘時(shí)間mus.’),
ylabel(‘幅值’)
在title中的字符串表現(xiàn)的是 Aeαt sinβt α<<β
斜體Ae 上標(biāo)斜體αt 斜體βt 斜體α 斜體β
(4). 在圖形中添加圖例框(Add legend in the figure)
legend(字符串1,字符串2,…)
例:在當(dāng)前圖形中添加圖例說明。
x=0:pi/10:2pi;
y1=sin(x);
y2=0.6sin(x);
y3=0.3sin(x);
plot(x,y1,x,y2,’-o’,x,y3,’-’)
legend( ‘曲線1’,‘曲線2’,‘曲線3’)
legend(‘boxoff’)
legend函數(shù)的其他功能見(表 6—8)
(1). 圖線的形式: (style of plot)
四種線形: 實(shí)線’-’,虛線’–’, 點(diǎn)線’:’,點(diǎn)劃線’-’.
標(biāo)記點(diǎn)類型:點(diǎn)’.’, 圓’o’, 加號(hào)’+’, 星號(hào)’*’, x符號(hào)’x’, 方形’s’, 菱形’d’, hexagram ‘h’
命令:plot(x,y,’—’), plot(x1,y1,’:’,x2,y2,’’)
例1:選擇不同的線形繪圖。
t=0:pi/100:2pi; y=sin(t); y2=sin(t-0.25); y3=sin(t-0.5);
plot(t,y,’-’,t,y2,’-’,t,y3,’:’)
例2:選擇不同的標(biāo)記點(diǎn)繪圖。
t=0:pi/20:2*pi; x=t.^3; y=sin(t); plot(x,y,‘o’)
(2). 線的顏色(color of plot)
可選顏色: 紅r,綠g, 藍(lán)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)
設(shè)置圖線的寬度 : ‘LineWidth’
標(biāo)記點(diǎn)的邊緣顏色: ‘MarkerEdgeColor’
填充顏色 : ‘MarkerFaceColor’
標(biāo)記點(diǎn)的大小 : ‘MarkerSize’
例: 設(shè)置圖線的線形、顏色、寬度、標(biāo)記點(diǎn)的顏色及大小。
t=0:pi/20:pi; y=sin(4*t).*sin(t)/2;
plot(t,y,’-bs’,‘LineWidth’,2,‘MarkerEdgeColor’,‘k’, ‘MarkerFaceColor’, ‘y’,‘MarkerSize’,10);
總結(jié)
以上是生活随笔為你收集整理的matlab命令及海洋作图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: v-slot vue2.6新增指令使用指
- 下一篇: matlab损耗函数曲线,MATLAB