一、線圖
plot 函數用來創建x和y值的簡單線圖。
x = 0 : 0.05 : 30; %從0到30,每隔0.05取一次值
y = sin(x);
plot(x,y,'LineWidth',2) %若(x,y,'LineWidth',2)可變粗
xlabel("橫軸標題")
ylabel("縱軸標題")
grid on %顯示網格
axis([0 20 -1.5 1.5]) %設置橫縱坐標的范圍,前兩個是x,后兩個是y
多組函數顯示在同一張圖
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
axis([0 20 -2 2])
二、條形圖
bar 函數創建垂直條形圖
barh 函數用來創建水平條形圖
t = -3:0.5:3; %橫坐標
p = exp(-t.*t); %指數函數,e的-t平方
bar(t,p)
barh(t,p)
三、極坐標圖
polarplot 函數用來繪制極坐標圖
theta = 0 : 0.01 : 2 * pi; %角度
% abs 求絕對值或復數的模
radi = abs(sin(7*theta).*cos(10*theta));
polarplot(theta,radi) %括號內是弧度和半徑
四、散點圖
scatter 函數用來繪制x和y值的散點圖
Height = randn(1000,1); %1000行一列,randn隨機函數調用,橫坐標
Weight = randn(1000,1); %縱坐標
scatter(Height,Weight)
xlabel('Height')
ylabel('Weight')
?五、三維曲面圖
surf 函數可用來做三維曲面圖。一般是展示函數z = z(x,y)的圖像。
首先需要用meshgrid創建好空間上(x,y)點
[X,Y] = meshgrid(-2:0.2:2);
%Z = X.^2 + Y.^2
Z = X.*exp(-X.^2-Y.^2); %.*就是相乘
surf(X,Y,Z);
colormap hsv %圖形的固定顏色
%colormap 設置顏色,可跟winter、summer等
%colorbar %顏色條柱代表的區間
六、內嵌子圖
使用subplot函數可以在同一窗口的不同子區域顯示多個繪圖
theta = 0:0.01:2*pi;
radi = abs(sin(2*theta).*cos(2*theta));
Height = randn(1000,1);
Weight = randn(1000,1);subplot(2,2,1);surf(X.^2);title('1st'); %兩行兩列,第一個圖是三維曲面圖
subplot(2,2,2);surf(Y.^3);title('2nd');
subplot(2,2,3);polarplot(theta,radi);title('3rd');
subplot(2,2,4);scatter(Height,Weight);title('4th');