一、Basic plotting
1.plot()
plot(x,y) :x圖片中點的橫坐標,y圖片中點的縱坐標
plot(y) :y圖片中點的縱坐標,x圖片中點的橫坐標默認為1,2,3,4,5........
plot(cos(0:pi/20:2*pi))
y —縱坐標的范圍是0~2,每個間隔
/ 20
2. hold on / off
① 無 hold on / off
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
只畫,不畫
,因為
將
的圖片覆蓋
② 有?hold on / off
hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off
和
的圖像疊加
3. Plot Style
plot(x,y, ' str ')
hold on
plot(cos(0:pi/20:2*pi),'or--');
plot(sin(0:pi/20:2*pi),'xg:');
hold off
str 可以是以下三種類型,' str '里面填寫每列右邊一列
linsepec()可以查找更多
4. legend()
hold on
x = 0:0.5: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,'or--',x,h,'xg:',x,w,'bd-',x,g,'c^-');
% plot(cos(0:pi/20:2*pi),'or--');
% plot(sin(0:pi/20:2*pi),'xg:');
% 上面兩條線可以聯合寫成下面這種形式
% plot(x,cos(0:pi/20:2*pi),'or--',x,sin(0:pi/20:2*pi),'xg:');
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
hold off
5. title()and ?label()
title()、xlabel()、ylabel()、zlabel()
hold on
x = 0:0.5:4*pi;
y = sin(x); h = exp(-x);
plot(x,y,'or--',x,h,'xg:');
% plot(cos(0:pi/20:2*pi),'or--');
% plot(sin(0:pi/20:2*pi),'xg:');
% 上面兩條線可以聯合寫成下面這種形式
% plot(x,cos(0:pi/20:2*pi),'or--',x,sin(0:pi/20:2*pi),'xg:');
title('Fuction Plots of sin(t) and e^{-x}');
xlabel('x = 0 to 4\pi');
ylabel('value of sin(x) and e^{-x}');
legend('sin(x)','e^{-x}');
hold off
在 圖片x軸,y軸和標題上顯示公式時需要 LaTex 語言
6. text()and annotation()
① line( [2,2] , [0,2^2^sin(2)] )
兩點確定一條直線,[ 2 , 2 ]兩點的橫坐標,[ 0?, 2^2^sin(2)?]兩點的縱坐標
② text(0.25,2.5,str,'Interpreter','latex');
圖片中顯示的標題使用LaTex語言
③ annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);
添加箭頭,箭頭的兩個點橫坐標分別占圖幅的0.32和0.5,縱坐標分別占圖幅的0.6和0.4
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]);
7. Fiegure Adjustment
?set( gcf,' Color ',[1,1,1] )
gcf 是 Figure object 的句柄,設置Figure object 的顏色為黑色:'Color',[1,1,1]
x = linspace(0,2*pi,1000);
y = sin(x);
plot(x,y);
set(gcf,'Color',[1,1,1]);
8. Figure Properties
9. Handle? of An Object
figure 的句柄是關鍵字 gcf ,Axes 的句柄是 gca,
獲得Line句柄的語句是 h? = plot(x,y);
其他句柄列表如下
10. Fetching and Modifying Properties
得到設置屬性get()和設置屬性set()
x = linspace(0,2*pi,1000);
y = sin(x);
h = plot(x,y);
get(h) #得到Line(h)的句柄,可通過查看變量h來看
set(gcf,'Color',[1,1,1]);
二、Getting Object Properties
get(gca);get(gcf);h = plot(x,y),get(h)
1. Setting Axes Limits
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
XLim設置的是 x 軸的范圍 0 ~ 2,YLim設置的是 y?軸的范圍 -1.2 ~ 1.2
或者也可以用下面這種寫法
xlim([0,2*pi]);
ylim([-1.2,1.2]);
2. Setting Font and Tick of Axes
x = linspace(0,2*pi,1000);
y = sin(x);
h = plot(x,y);
get(h)
set(gcf,'Color',[1,1,1]);
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-1.2,1.2]);
set(gca,'FontSize',25);
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',0:90:360);
set(gca,'FontName','Latex');
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});
set(gca,'FontSize',25); 設置坐標軸字體大小為25
set(gca,'XTick',0:pi/2:2*pi);?設置坐標軸范圍是0~2,間隔是
/2.
set(gca,'XTickLabel',0:90:360);?設置坐標軸等間隔下的表示數字
set(gca,'FontName','Latex');?設置坐標軸字體格式是Latex語言
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});?設置坐標軸輸出內容 0 ~ 2
3. Line Specification
x = linspace(0,2*pi,1000);
y = sin(x);
h = plot(x,y);
get(h)
set(h,'LineStyle','-.', ...'LineWidth',7.0,'Color','g');
set(h,'LineStyle','-.', 'LineWidth',7.0,'Color','g');
設置LineStyle是:' -. ' 虛實線
設置LineWidth是:0.7,虛實線的粗細是0.7
設置Color虛實線的顏色是:綠色
4. Maker Sepecifiction
坐標軸下面的字體大小 FontSize設置為18;
線寬LineWidth是2,圖形標記邊緣顏色MarkerEdgeColor是黑色,圖形標記內部顏色MarkerFaceColor是綠色,圖形標記MarkerSize的大小是10。
x = rand(20,1);
set(gca,'FontSize',18);
plot(x,'-md','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
xlim([1,20]);
practise
hold on
x = linspace(1,2);
f = x .* x;
g = sin(2 * pi .* x);
plot(x,f,'-k','LineWidth',2);
plot(x,g,'or','MarkerFaceColor','m');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment #1');
legend('t^{2}','sin(2\pit)');
hold off
5. Multiple Figures
figure,plot(x,y1);? 畫圖,該語句需要使用兩次,以免新畫的圖覆蓋原來畫的圖
x = -10:0.1:10;
y1 = x .* 2 -8;
y2 = exp(x);
figure,plot(x,y1);
figure,plot(x,y2);
6. Figure Position and Size
figure(' Position ',[ left,bottom,width,height ] );
7. Several Plots in One Figure
subplot(m,n,1);
8. subplot()
t = 0:0.1: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;
nomal 橫縱坐標軸的比例恢復默認的自動調整模式
square 畫出來的圖片是方形的;
equal 畫出來的圖片橫縱坐標比例是一樣的;
equal tight 畫出來的圖片是橫縱比例一樣,且無多余留白的;
9. Control of Grid,Box,and Axis
grid on/off 畫圖背景是否有表格
box on/off 畫圖背景圖片是否有邊框
axis on/off 畫圖背景是否有坐標軸
10. saving Figure into Files
saveas(gcf,'filename','jgp')