matlab 初級畫圖express
1.plot()windows
plot(x,y) plots each vector pairs (x,y)數組
畫圖函數畫出每一個點 每組變量ide
plot (y) plots each vector pairs(x,y),where x=[1...n],n=length(y)函數
僅有一個變量時 僅畫出 當x=整數點時的對應y值得點工具
example:字體
plot (cos(0:pi/20:2*pi));fetch
從0-2π 間隔爲pi/20的 那些cos值spa
x=0:1/1000:2*pi; 1/1000爲步進值 越小越光滑 3d
y=sin(x);
plot(x,y);
保持當前圖的基礎上再另加其餘的圖 須要用到指令 hold on
關閉功能爲hold off
例如 hold on
plot(sin(0:2pi));
plot(cos(0:2pi));
hold off
plot style
hold on
plot (cos(0:pi/20:2*pi),'or'); 對照上表中 or表示紅色的圓圈
plot(sin(0:pi/20:2*pi).'xg'); xg 表示綠色的XX
hold off
以下圖所示
legend() 圖例
1.add legend to graph
legend('L1',....)
1.position adjustment 位置調節
例如:
x=0:0.05:4*pi;
y=sin(x);
h=cos(x);
w=1./(1+exp(-x));
g=(1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
以下圖所示
title() and ?label() 標題和座標軸
title() xlabel() ylabel() zlabel()
xlabel('str');
ylabel('str');
title('str');
str中的轉義字符 例如pi 表示成π 則須要 輸入\pi
表示e的-x次方 須要輸入 e^{-x} 大括號表示特殊的字元
text() and annotation()
text with mathematical expression using LaTex
x=linspace(0,3);
y=x.^2.*sin(x);
plot(x,y);
line([2,2],[0,2^2*sin(2)]);
str='$$ \int_{0}^{2} x^2\sin(x) dx $$';
text(0.25,2.5,str,'Interpreter','latex');
annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
1.
linspace是Matlab中的均分計算指令,用於產生x1,x2之間的N點行線性的矢量。其中x一、x二、N分別爲起始值、終止值、元素個數。若默認N,默認點數爲100。 X=linspace(1,100)
3.\int 就是積分符號 _{0}爲下標 ^{2} 表示上標 ,x^2\sin(x)dx 爲內容爲x的平方sin(x)dx ,$$爲語法格式
4.'arrow','X',[0.32,0.5],'Y',[0.6,0.4] 其中的0.32和0.5爲箭頭的座標
練習
plot f as a black line and g as aseries of red circles for the range t=1 to 2 in on figure
f=t^2 and g=sin(2πt)
Label each axis, and add title and legend
hold on
t=linspace(1,2);
f=t.^2;
g=sin(2*pi*t);
plot(t,f,'k-',t,g,'ro');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment \#1');
legend('t^{2}','sin(2\pit)');
hold off
圖示以下
Figure Adjustment
several properties:
font font size line width axis limit tick position tick label
字體 大小 線條寬度 軸的極限 步進位置 步進標籤
first step what kinds of objects in the graph?
a figure is composed of many objects?
figure object axes object line object
層級關係
hierarchy--》figure--》axes----》(line;text;surface;....) 對象的層級關係 分別是 圖形 座標軸 (線 文本 表面...)
figure 的屬性 編輯---》圖形屬性 調出界面
modifying properties of an object
strategy:
1. identify the handle of an object 定義對象的句柄(變量實例);
2. fetch or modify the object 's properties 獲取或修改對象的屬性
example
1.upon creantion:
h=plot(x,y);
get(h);
例子中的h指向line
utility functions 工具函數
gca return handle of current axes 當前的座標軸實例句柄
gcf return handle of the current figure 當前圖像的實例句柄
allchild find all children of specified objects 全部特殊指定對象的孩子
ancestor find ancestor of graphics object 圖像中對象的祖先
delete delete an object
findall find all graphics objects
fetching modifying properties
to fetch properties ,
get()
to modify properties
set()
geting object properties
獲取對象的屬性
get(h);
get(gca);
setting axes limites
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
alternative:
xlim({0,2*pi});
ylim({-1.2,1.2});
setting font and tick of axes
set(gca,'FontSize ',25);
set(gca,'XTick',0:pi/2:2*pi); 給定範圍和步進
set(gca,'XTickLabel',0:90:360); 以角度的形式替代上面的pi值
set(gca,'FontName','symbol'); 設置顯示字類型 符號(字符串顯示)
set(gca,'XTickLabel',{0",'p/2','p','3p/2','2p'}); 將座標數值用字符顯示 將字符列出
line specification
line style and width
set(h,'LineStyle','-.',...'LineWidth',7.0,'Color','g'); 其中 ...表示 其他的參數能夠輸入 若是沒有其他參數 則...去掉
alternative:
plot(x,y,'-.g','LineWidth',7.0);
刪除delete(h);
Marker specification
face and edge colors of the markder
x=rand(20,1); 隨機生成矩陣 數值大於等於0 20個數
set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10); k爲黑色 markeredgecolor 黑色邊框 ;markerfacecolor 邊框範圍內的顏色爲g 綠色 如圖所示
xlim([1,20]);
exercise:
圖1代碼以下
set(gca,'XTick',1:1/5:2);
set(gca,'YTick',-1:1/2:4);
t=linspace(1,2);
y=t.^2;
f=sin(2*pi*t);
plot(t,y,'k-',t,f,'ro');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)','Location','northwest');
圖2代碼以下
set(gca,'XTick',1:1/5:2,'YTick',-1:1:4,'FontSize',15);
t=linspace(1,2);
y=t.^2;
f=sin(2*pi*t);
hold on
m=plot(t,y,'k-');
n=plot(t,f,'ro');
set(n,'MarkerEdgeColor','r','MarkerFaceColor','b');
set(m,'LineWidth',4);
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^2','sin(2\pi t)','Location','northwest');
hold off
Multiple Figures
Create a figure windows by calling figure 調用函數 建立圖像
example:
x=-10:0.1:10;
y1=x.^2-8;
y2=exp(x);
figure, plot(x,y1);
figure,plot(x,y2);
分別畫出兩個圖像
be careful when using the gcf handle where there exists multiple figures
存在多個figure 注意 gcf函數時調用的那個句柄
Figure Position and Size
figure('Position',[left,bottom,width,height]);
example:
x=-10:0.1:10;
y1=x.^2-8;
figure('position',[10,10,1000,700]); 窗口在屏幕中的位置會變化
plot(x,y1);
Several Plots in One Figure 一個窗口中多個圖像
Several small plots "in a figure"
subplot(m,n,1); 其中的m n爲矩陣中的行數和列數 1爲位置
example:
t=0:0.2:2*pi;
x=3*cos(t);
y=sin(t);
subplot(2,2,1);plot(x,y);axis normal
subplot(2,2,2);plot(x,y);axis square
subplot(2,2,3);plot(x,y);axis equal
subplot(2,2,4);plot(x,y);axis equal tight
如圖所示
Control of Grid,Box,and Axis
Saving Figures into Files
命令:saveas(gcf,'<filename>','<formattype>');
選項