翻譯自 MATLAB 官方文檔
- 建立二維圖形并自定義線條
- LineSpec:自定義顔色、線型和标記
- 指定坐标軸範圍
- 更改坐标軸範圍
- 使用半自動坐标軸範圍
- 還原為預設範圍
- 反轉坐标軸方向
- 顯示通過原點的軸線
- 删除坐标輪廓區
- 向圖形添加标題、坐标軸标簽和圖例(Title, axis & legend)
- 建立包含雙 y 軸的圖(double Y axis)
- 合并多個繪圖
- 使用hold on 在同一坐标系下合并
- 使用 subplot 函數在同一個window 上繪制不同的坐标區
- 添加超級标題
建立二維圖形并自定義線條
x = linspace(,*pi,);
y = sin(x);
figure
plot(x,y)
figure % new figure window
stairs(x,y) % 階梯圖
y1 = sin(x);
y2 = sin(x-pi/);
figure
plot(x,y1,x,y2) % 同時繪制多個線條
figure
plot(x,y1,'--g',x,y2,':r') % 給定 LineSpec
使用 figure 指令打開一個新圖窗視窗。如果未打開新圖窗視窗,則預設情況下,MATLAB 會清除現有圖形并在目前圖窗中繪制。
LineSpec:自定義顔色、線型和标記
三者組合成為不同樣式的線條
-
是帶有圓圈标記的紅色虛線'--or'
更多細節,參看 plot – MATLAB.
指定坐标軸範圍
可以設定 x 軸、y 軸和 z 軸範圍,以控制資料在坐标區上的顯示位置。您也可以更改 x 軸線和 y 軸線的顯示位置(僅适用于二維繪圖),或反轉值沿每條軸遞增的方向。
更改坐标軸範圍
使用
xlim
和
ylim
函數指定坐标軸範圍。對于三維繪圖,加上
zlim
函數。将
[min max]
形式的二進制素向量傳遞給函數。
x = linspace(-,,);
y = sin(*x)./exp(x);
plot(x,y)
xlim([ ])
ylim([- ])
使用半自動坐标軸範圍
将 x 軸範圍最大值設為 0,y 軸範圍最小值設為 -1。其他範圍則由 MATLAB 選擇。對于自動計算的範圍最小值或最大值,分别使用 -inf 或 inf。
[X,Y,Z] = peaks;
surf(X,Y,Z)
xlabel('x-axis')
ylabel('y-axis')
xlim([-inf ])
ylim([- inf])
還原為預設範圍
xlim auto
ylim auto
zlim auto
反轉坐标軸方向
通過設定 Axes 對象的 XDir 和 YDir 屬性,可控制 x 軸和 y 軸值遞增的方向。這些屬性可以設定為
'reverse'
或
'normal'
(預設值)。使用 gca 指令可通路 Axes 對象。
stem(:)
ax = gca;
ax.XDir = 'reverse';
ax.YDir = 'reverse';
顯示通過原點的軸線
預設情況下,x 軸和 y 軸沿坐标區的外邊界顯示。通過設定 Axes 對象的 XAxisLocation 和 YAxisLocation 屬性來更改軸線位置,以使軸線在原點 (0,0) 處交叉。将 XAxisLocation 設定為 ‘top’、’bottom’ 或 ‘origin’。将 YAxisLocation 設定為 ‘left’、’right’ 或 ‘origin’。這些屬性僅适用于二維視圖中的坐标區。
x = linspace(-,);
y = sin(x);
plot(x,y)
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
删除坐标輪廓區
向圖形添加标題、坐标軸标簽和圖例(Title, axis & legend)
title('Graph of Sine and Cosine Between -2\pi and 2\pi') % 添加标題
xlabel('-2\pi < x < 2\pi') % x-axis label
ylabel('sine and cosine values') % y-axis label
legend('y = sin(x)','y = cos(x)') % 圖例
legend('y = sin(x)','y = cos(x)','Location','southwest')
legend('y = sin(x)','y = cos(x)','Location','southwestoutside')
文本中用到的希臘字母,用 LATEX L A T E X 書寫。
建立包含雙 y 軸的圖(double Y axis)
x = linspace(,);
y = sin(x/);
yyaxis left
plot(x,y);
r = x.^/; // elementwise square
yyaxis right
plot(x,r);
yyaxis left
title('Plots with Different y-Scales')
xlabel('Values from 0 to 25')
ylabel('Left Side')
yyaxis right
ylabel('Right Side')
hold on
yyaxis left
y2 = sin(x/);
plot(x,y2);
y3 = sin(x/);
plot(x,y3);
yyaxis right
load count.dat;
m = mean(count,);
e = std(count,,);
errorbar(m,e)
hold off
使用
hold on
指令,會在目前figure繼續繪制圖形。新繪圖不會重置标題或軸标簽等坐标區屬性,但是,坐标區範圍和刻度值會進行調整以适應新資料。
對于雙Y軸而言,新圖采用和對應 y 軸一樣的顔色代表同一族曲線,但循環使用線型序列,以示區分。
hold on
同時影響左右兩側。
對于單Y軸而言,新繪圖将根據坐标區的 ColorOrder 和 LineStyleOrder 屬性循環使用顔色和線型。
之後,下一個新繪圖将清除現有繪圖,并重置标題等坐标區屬性。
hold off
合并多個繪圖
使用hold on 在同一坐标系下合并
x = linspace(,,);
y1 = sin(x);
figure
plot(x,y1)
title('Combine Plots')
hold on
y2 = sin(x/);
plot(x,y2)
y3 = *sin(x);
scatter(x,y3)
hold off
使用 subplot 函數在同一個window 上繪制不同的坐标區
figure
ax1 = subplot(,,);
x = linspace(,,);
y1 = sin(*x);
plot(ax1,x,y1)
title(ax1,'Subplot 1')
ylabel(ax1,'Values from -1 to 1')
ax2 = subplot(,,);
y2 = rand(,);
scatter(ax2,x,y2)
title(ax2,'Subplot 2')
ylabel(ax2,'Values from 0 to 1')
添加超級标題
f = figure;
p = uipanel('Parent',f,'BorderType','none');
p.Title = 'My Super Title';
p.TitlePosition = 'centertop';
p.FontSize = ;
p.FontWeight = 'bold';
subplot(,,,'Parent',p)
x = linspace(,,);
y1 = sin(*x);
plot(x,y1)
title('Subplot 1')
subplot(,,,'Parent',p)
y2 = rand(,);
scatter(x,y2)
title('Subplot 2')