情節函數
要建立具有兩個y軸的畫圖,請利用畫圖函數。
該函數接管兩組x,_y_data。
第一組x,_y_data被繪制在左邊的y軸上,第二組x,_y_data被繪制在右邊的y軸上。
號令行鍵入:
x1 = 0:0.01:20; % data for the left y-axis
y1 = 200*exp(-0.05*x1).*sin(x1);
x2 = 0:0.05:20; % data for the right y-axis
y2 = 0.8*exp(-0.5*x2).*sin(10*x2);
figure
plotyy(x1,y1,x2,y2)
按“Enter”鍵。
如圖1所示。
 更改軸屬性
plotyy函數建立兩個y軸。
要獲取兩個軸對象,請利用一個輸出參數挪用該函數。
該函數返回的值是一個包含兩個軸對象的數組。
號令行鍵入:
ax = plotyy(x1,y1,x2,y2)
按“Enter”鍵。
如圖2所示。
 若要自界說畫圖的外不雅,請利用點符號語法對象更改肆意軸的屬性值。
屬性名。
號令行鍵入:
ax(1).YTick = [-200 -100 0 100 200]; % change the ticks for the left y-axis
ax(2).YGrid = 'on'; % turn on the grid for the right y-axis
按“Enter”鍵。
如圖3所示。
 為每個軸選擇畫圖函數
plotyy函數有可選的參數,許可您指定在繪制每個y軸的數據時要利用的函數類型。
號令行鍵入:
x = 1:11;
data1 = [4889 5273 5382 5173 4860 4675 4313 4059 3855 3608 3297];
data2 = [16.4 17.4 17.4 16.5 15.4 14.7 13.5 12.5 11.7 10.8 9.7];
plotyy(x,data1,x,data2,'bar','plot') % left y-axis with bar, right y-axis with plot
按“Enter”鍵。
如圖4所示。
 點竄繪制的對象的屬性
第二個和第三個輸出參數別離是為左軸和右軸建立的對象。
鄙人面的示例中,第二個輸出是為左y軸建立的條形對象,第三個輸出是為右y軸建立的線性對象。
號令行鍵入:
[ax,h1,h2] = plotyy(x,data1,x,data2,'bar','plot')
按“Enter”鍵。
如圖5、6所示。
 
 一旦您有了對象,您就可以設置它們的屬性來自界說情節的外不雅。
號令行鍵入:
h1.FaceColor = [0.8, 0.8, 0.8]; % change the bar colors to light gray
h2.LineWidth = 2; % change the thickness of the line
按“Enter”鍵。
如圖7所示。
 END0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!