台湾国立大学郭彦甫Matlab教程笔记(10) basic plotting下
figure adjustment圖形調整
1.several properties:
font字體
font size 字體大小
line width
axis limit
tick position刻度位置
tick label 刻度
graphical objects繪圖物件
1.a figure is composed of many objects
figure object 整個圖像 \ axes object坐標系統 \ line object線條
這幾點的關系:
hierarchy ,parent關系,繼承關系
圖上有坐標軸系,坐標軸系上有line、text、surface等等。
例程:會看到一張圖
下面說一下繪圖物件的屬性
figure properties 圖像屬性
以上面例程為例
1.繪制圖像出來之后,點擊編輯,彈出,點擊查看當前對象屬性
2.彈出的物件屬性如下圖所示
3.為了了解更多一點 圖形的屬性,我們點擊 更多屬性。這里屬性更加豐富,可以查看我們繪制的圖 line object 還有 點擊 坐標軸, 就會顯示axes的屬性信息。
所以說,matlab對圖形很nice
知道了繪圖物件的屬性,接下來就要去修改屬性
modifying properties of an object修改屬性
strtegy:
1.identify the’handle’ of an object ,handle is like its ID number
2fetch or modify the object’s properties
1. identifying the handle of an object物件的辨識碼
第一種方法
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()
執行下面代碼,看看是什么情況
執行完,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: 6Parent: [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信息
再來執行
get(gca)gca:handle of axis
效果:也會顯示axis的詳細信息
2.modify properties修改屬性
fetching or modifying properties
to fetch properties ,use
get()
to modify properties ,use
set()
然后,下一個問題,如果我們想要修改 x-axis的limits ,即修改x軸的范圍和起止點
設定axis 的limits
setting axes limits
代碼code:
解釋:第一個參數是handle,表示是哪一個物件,是軸,還是線。第二個參數是 那一部分;第三個參數是改成什么。
第一句是把x軸改成0到2pi
還有兩個指令達到同樣的效果,但是呢,上面兩個指令對理解matlab中handle的原理和運作方式有幫助
alternative:
上面我們已經會修改坐標軸的范圍limits,下面我們修改字體和 坐標軸的刻度
setting font and tick of axes
修改字體后
問題:在畫三角函數的時候,我們希望坐標軸tick與pi有關
代碼:
把坐標軸變成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 符號。
本文的參考圖文本中的希臘字母和特殊字符
從創建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,設定一個內容
例程:
例程執行的結果: 線的風格是實點線;線寬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 on plot(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
看例程:
運行結果:
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
subplot(m,n,1);范例程式,需要畫四個圖在一個figure中
t=0:0.1:2*pi;x=2*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執行結果:
上圖中最正確的圖是左下角 :axis equal 兩個坐標軸刻度一樣
axis square tight 是讓邊框切齊于畫的圖
axis square 是讓圖片顯示呈現正方形,但是會讓繪制出來的東西變形
Control of Grid,Box,and Axis 控制格線
想把坐標關掉
axis off
把x軸和y軸上半部分關掉 box off
柵格線打開 grid on
繪圖存儲 Saving Figures into Files
saveas(gcf,'<文件名>','formatytype');另外,存高解析度的圖要用print。
總結
本文主要記錄了繪圖的一部分基礎知識。
包括繪圖物件的繼承關系,物件的屬性獲取與修改,繪制多幅圖,顯示柵格,保存圖片。
保存圖的時候沒有例程。
總結
以上是生活随笔為你收集整理的台湾国立大学郭彦甫Matlab教程笔记(10) basic plotting下的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 台湾国立大学郭彦甫Matlab教程笔记(
- 下一篇: 年利率和年化利率的区别