today:
1.basic plotting
2.graphical objects properties
matlab has a powerful plotting engine that can generate a wide variety of plots
plot from 『『data』』
1.matlab does not understand functions f(t)=sin(2pit)
2.strategies:
generate the numetic values
data point
function
plot()plot(x,y) plots each vector pairs(x , y)
matlab會 refresh ,把之前的圖給清掉,如果 想保留,用 指令 hold on
下面是sin和cos的圖像
plot(cos(0:pi/20:2*pi));hold on plot(sin(0:pi/20:2*pi));plot styleplot(x,y,『str』)plots each vector pairs(x,y ) using the format defined in str (check linespec)
畫 marker
如果想要sin函數 用圈圈,顯示紅色 需要這樣添加plot(sin(0:pi/20:2*pi),'or');o表示circle圈圈,r表示red紅色
如果想要cos函數用叉叉,綠色。則如下:xgplot(cos(0:pi/20:2*pi),'xg');
legend()函數add legend to graph 添加圖標 legend(『L1』,『L2』,『L3』…)
position adjustment畫圖:
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,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');分析:4個函數,用一個plot畫出來。
'bd-'表示:blue diomond solid line 意思是藍色的,鑽石(稜形)的,實線
'gp:'表示:green five-point star dotted line 意思是 綠色的五角星表示的點線
'ro-'表示:red circle solid line 紅色的圓圈的實線
'c^-'表示:cyan up triangle solid line 青色,上三角,實線
沒有legend的時候 ,
現在為了區分這四個函數,需要添加legend圖標legend('sin(x)','cos(x)','Sigmoid','Causs function');效果圖:
title() and lable()title()
xlable()
ylable()
zlable()
例程:
x=0:0.1:2*pi; y1=sin(x); y2=exp(-x);%兩個函數plot(x,y1,'--*',x,y2,':o');%畫圖xlabel('t=0 to 2\pi');%x軸ylabel('values of sin(x) and e^{-x}');%y軸title('Function plots if sin(x) and e^{-x}');%標題legend('sin(x)','e^{-x}');%曲線圖標效果:
text() and annotation()
注意:反斜槓\ +pi 這樣顯示的話 直接是Π ,在字符串裡面問題:積分符號怎麼顯示呢?
text with mathematical expression using Latex
這個積分符號是用text()函數寫的 ,text函數前兩個參數是坐標,畫在哪兒。第三個參數是 積分 這個變量str,後面是一個插值
slash 反斜槓 \int 是∫符號
積分的表示:str=' $$ \int_ {0}^{2} x^2\sin(x) dx$$';前後都有兩個$$,然後是積分符號,\int_ 然後是上下限 {0}^{2},之後就是一個被積函數 x^2\sin(x) dx
箭頭使用annotation 畫的 ,arrow, X的位置變化 ,Y的位置變化
代碼: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]);效果圖;積分的顯示
作業題:我的練習代碼:
t=1:0.01:2;f=t.*t;g=sin(2*pi*t);%畫圖plot(t,f,'k-',t,g,'or');%坐標軸和標題xlabel('Time (ms)');ylabel('f(t)');title('Mini Assignment #1');%標註函數legend('t^2','sin(2\pit)');效果圖:
[總結】以上記錄了基本的繪圖知識。主要是text()函數顯示積分,還有一些畫線的style風格。
figure adjustment圖形調整1.several properties:
graphical objects繪圖物件
font字體
font size 字體大小
line width
axis limit
tick position刻度位置
tick label 刻度1.a figure is composed of many objects
figure object 整個圖像 \ axes object坐標系統 \ line object線條這幾點的關係:
hierarchy ,parent關係,繼承關係
圖上有坐標軸系,坐標軸系上有line、text、surface等等。
例程:會看到一張圖x=linspace(0,2*pi,1000);y=sin(x);plot(x,y);set(gcf,'Color',[1 1 1]);下面說一下繪圖物件的屬性
figure properties 圖像屬性以上面例程為例
1.繪製圖像出來之後,點擊編輯,彈出,點擊查看當前對象屬性
2.彈出的物件屬性如下圖所示
3.為了了解更多一點 圖形的屬性,我們點擊 更多屬性。這裡屬性更加豐富,可以查看我們繪製的圖 line object 還有 點擊 坐標軸, 就會顯示axes的屬性信息。
所以說,matlab對圖形很nice知道了繪圖物件的屬性,接下來就要去修改屬性
modifying properties of an object修改屬性
strtegy:
1. identifying the handle of an object物件的辨識碼
1.identify the』handle』 of an object ,handle is like its ID number
2fetch or modify the object’s properties第一種方法
upon creation: h=plot(x,y);物件的標識碼給h,這是一個數
這種方法,使用 的時候需要找到 utility functions
gca 表示的是坐標軸的handle,gcf是整個圖figure這一層的handle第二種方法是get()函數
getting object properties
getting properties of a graphical object:get()
執行下面代碼,看看是什麼情況x=linspace(0,2*pi,1000);y=sin(x); plot(x,y);h=plot(x,y); get(h)執行完,matlab會回傳很多東西
AlignVertexCenters: 'off' Annotation: [1×1 matlab.graphics.eventdata.Annotation] BeingDeleted: 'off' BusyAction: 'queue' ButtonDownFcn: '' Children: [0×0 GraphicsPlaceholder] Clipping: 'on' Color: [0 0.4470 0.7410]%顏色 CreateFcn: '' DeleteFcn: '' DisplayName: '' HandleVisibility: 'on' HitTest: 'on' Interruptible: 'on' LineJoin: 'round' LineStyle: '-' %線是solid line 實線 LineWidth: 0.5000%線寬也給出 Marker: 'none' MarkerEdgeColor: 'auto' MarkerFaceColor: 'none' MarkerIndices: [1×1000 uint64] MarkerSize: 6 Parent: [1×1 Axes] PickableParts: 'visible' Selected: 'off' SelectionHighlight: 'on' Tag: '' Type: 'line' UIContextMenu: [0×0 GraphicsPlaceholder] UserData: [] Visible: 'on' XData: [1×1000 double] XDataMode: 'manual' XDataSource: '' YData: [1×1000 double] YDataSource: '' ZData: [1×0 double] ZDataSource: ''h是畫的sin()函數 的handle信息
再來執行
gca:handle of axis
2.modify properties修改屬性
效果:也會顯示axis的詳細信息fetching or modifying properties
to fetch properties ,use
get()
to modify properties ,use
set()然後,下一個問題,如果我們想要修改 x-axis的limits ,即修改x軸的範圍和起止點
設定axis 的limits
setting axes limits
代碼code:set(gca,'XLim',[0,2*pi]);set(gca,'YLim',[-1.2,1.2]);解釋:第一個參數是handle,表示是哪一個物件,是軸,還是線。第二個參數是 那一部分;第三個參數是改成什麼。
第一句是把x軸改成0到2pi
還有兩個指令達到同樣的效果,但是呢,上面兩個指令對理解matlab中handle的原理和運作方式有幫助
alternative:xlim([0,2*pi]);ylim([-1.2,1.2])上面我們已經會修改坐標軸的範圍limits,下面我們修改字體和 坐標軸的刻度
setting font and tick of axes
修改字體後
問題:在畫三角函數的時候,我們希望坐標軸tick與pi有關
代碼:set(gca,'xtick',0:pi/2:2*pi);set(gca,'xticklabel',0:90:360);把坐標軸變成0到2pi,中間間隔是pi/2.執行完第一行代碼,雖然變成了與pi相關,但是現實出來還是小數
執行完第二行代碼,顯示的是0到2pi,對應的角度值
這個離pi還是不對,接下來就是正式變成pi的過程
需要把font變成symbol ,在symbol中p表示pi
代碼:set(gca,『FontName』,『symbol』);set(gca,『xticklabel』,{『0』,『p/2』,『p』,『3p/2』,『2p』});執行這兩行代碼,matlab顯示警告,
現在matlab不再支持symbol字體,
那matlab如何在坐標軸上顯示希臘字母pi呢?第一,將這些位置指定為一個由遞增值組成的向量。這些值無需等距。
第二,還要更改關聯的標籤。並用一個字符向量元胞數組來指定刻度標籤。要在標籤中包含特殊字符或希臘字母 , 可使用 TeX 標記,例如用 \pi 表示pi 符號。xticks([0,pi/2,pi,3*pi/2,2*pi]); %位置指定為向量xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'});%關聯的標籤,用cell指定刻度標籤本文的參考圖文本中的希臘字母和特殊字符
從創建sin()函數坐標軸顯示pi的完整代碼;
x=linspace(0,2*pi,200);y=sin(x);h= plot(x,y);xticks([0,pi/2,pi,3*pi/2,2*pi]); %位置指定為向量xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'});%關聯的標籤,用cell指定刻度標籤顯示的結果:
line specification畫線特例line style and width:
指定一個property,設定一個內容
例程:set(h,'linestyle','-.','linewidth',7.0,'color','g');例程執行的結果:線的風格是實點線;線寬7.0,線的顏色:綠色
另一種方法:
plot(x,y,'-.g','linewidth',7.0);如果執行delete(h)會怎樣?
marker specification繪製點的特例
整幅圖都沒有了marker有兩個部分,一個是face,另一個是edge
face and edge colors of the marker例程:
x=rand(20,1);set(gca,'fontsize',18);plot(x,'-md','linewidth',2,'markeredgecolor','k','markerfacecolor','g','markersize',10);xlim([1,20]);執行結果:
課後作業:題目要求在圖片上,標註1,2,3的地方
我的練習:我把顏色改成了青色c,黑色線寬是7
原始碼:
t=1:0.01:2;f=t.*t;g=sin(2*pi*t);%繪圖hold onplot(t,g,'or','markerfacecolor','c');plot(t,f,'k-','linewidth',7);%設置字體、坐標軸set(gca,'fontsize',16);xlabel('Time (ms)');ylabel('f(t)');title('Mini Assignment #1');%顯示圖線標註legend('t^2','sin(2\pit)');練習結果:
multiple figures顯示多幅圖create a figure window by calling figure
在使用plot()函數之前先呼叫figure
看例程: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
當想要使用gcf獲取畫線的屬性的時候要當心,因為這個gcf指向的是最新的figure,要改變的就是當前的figure,之前的figure上的屬性是改的哦。想改的話,需要用別的方法。
figure positon and size顯示的位置和大小several plots in one figure多圖在一張圖顯示several small plots in a figure
範例程式,需要畫四個圖在一個figure中
t=0:0.1:2*pi;x=2*cos(t); y=sin(t);subplot(2,2,1); plot(x,y); axis normalsubplot(2,2,2); plot(x,y); axis squaresubplot(2,2,3); plot(x,y); axis equalsubplot(2,2,4); plot(x,y); axis equal tight執行結果:
上圖中最正確的圖是左下角 :
axis equal 兩個坐標軸刻度一樣
Control of Grid,Box,and Axis 控制格線
axis square tight 是讓邊框切齊於畫的圖
axis square 是讓圖片顯示呈現正方形,但是會讓繪製出來的東西變形繪圖存儲 Saving Figures into Files
想把坐標關掉
axis off
把x軸和y軸上半部分關掉 box off
柵格線打開 grid onsaveas(gcf,'<文件名>','formatytype');另外,存高解析度的圖要用print。
總結
本文主要記錄了繪圖的一部分基礎知識。
包括繪圖物件的繼承關係,物件的屬性獲取與修改,繪製多幅圖,顯示柵格,保存圖片。
保存圖的時候沒有例程。