matlab命令及海洋作图
常用命令
addpath(genpath(‘文件夾路徑’)) 將某個文件夾及其子路徑加入搜索路徑
rmpath(path) 刪除某一路徑
save path 將添加和刪除的路徑保存
squeeze(A) 刪除單一維度
m_map工具箱
m_map工具箱的應用,參考:
https://www.eoas.ubc.ca/~rich/map.html
主要用于地圖投影作圖,比起matlab自帶的contourfm等函數,bug和問題相對較少
matlab作矢量圖
m_quiver畫海流、風場等矢量時,不好畫refrence vector
推薦使用m_vec函數,reference vector的畫法基本就是在空白處再畫一個箭頭標上數字和單位即可,m_vec腳本開頭有一個詳細的實例可以參考。
對于模式輸出矢量如果太密,采用的方式是等間隔取格點來作圖
基本繪圖命令
1.曲線線型、顏色和標記點類型
plot(X1,Y1,LineSpec, …) 通過字符串LineSpec指定曲線的線型、顏色及數據點的標記類型。
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’)表示用點來畫圖,點的顏色是紅色。
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’) 圖例位置放在最佳位置
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) 來指定圖例標識框的位置
'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 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')圖形的控制與表現
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: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). 在一個已有的圖形上繪圖(Drawing a figure on the figure was existed)
hold on :在一個已有的圖形上繼續繪圖;
例:將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:2pi; figure(1);
subplot(2,1,1); plot(sin(t),2cos(t)); grid on %缺省狀態下的圖形比例
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.圖形標注(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: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(‘正弦函數圖形’,‘FontName’,‘隸書’,‘FontSize’,20)
(2). 圖中加注文本(Add text in the figure)
text(x,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(‘正弦函數圖形’,‘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 表示加一個向左的箭頭
rightarrow 表示加一個向右的箭頭
HorizontalAlignment 表示右對齊水平排列
gtext(‘字符串’): 在圖形窗口上用鼠標直接在指定的位置上加注文本。
例:
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(‘正弦函數圖形’,‘FontName’,‘隸書’,‘FontSize’,20)
gtext(‘MATLAB’)
(3). 指定TeX字符
例:在標題中指定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(‘時間mus.’),
ylabel(‘幅值’)
在title中的字符串表現的是 Aeαt sinβt α<<β
斜體Ae 上標斜體αt 斜體βt 斜體α 斜體β
(4). 在圖形中添加圖例框(Add legend in the figure)
legend(字符串1,字符串2,…)
例:在當前圖形中添加圖例說明。
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函數的其他功能見(表 6—8)
(1). 圖線的形式: (style of plot)
四種線形: 實線’-’,虛線’–’, 點線’:’,點劃線’-’.
標記點類型:點’.’, 圓’o’, 加號’+’, 星號’*’, x符號’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:選擇不同的標記點繪圖。
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);
總結
以上是生活随笔為你收集整理的matlab命令及海洋作图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: v-slot vue2.6新增指令使用指
- 下一篇: matlab损耗函数曲线,MATLAB