text小技巧
- 1. 常規使用(Method 1)
- 2. 在顯示畫面的相對位置(Method 2)
- 3. 舉個例子
1. 常規使用(Method 1)
text(x,y,txt)
2. 在顯示畫面的相對位置(Method 2)
text('string',‘ABC’,...'Units','normalized',...'position',[0.04,0.08],...'HorizontalAlignment','center',...'FontName','Times New Roman','FontSize',12,'FontWeight','Bold');
‘string’ - 對應要添加的文本內容txt
‘units’? - 位置和范圍單位
單位 | 描述 |
---|---|
‘data’(默認值) | 數據坐標。 |
'normalized' | 針對坐標區進行歸一化。坐標區的左下角映射到 (0,0),右上角映射到 (1,1)。 |
‘inches’ | 英寸。 |
‘centimeters’ | 厘米。 |
‘characters’ | 基于默認系統字體的字符大小。 - 字符寬度 = 字母 x 的寬度。 - 字符高度 = 兩個文本行的基線之間的距離。 |
‘points’ | 磅。一磅等于 1/72 英寸。 |
‘pixels’ | 像素。 從 R2015b 開始,以像素為單位的距離不再依賴 Windows? 和 Macintosh 系統上的系統分辨率: - 在 Windows 系統上,一個像素是 1/96 英寸。 - 在 Macintosh 系統上,一個像素是 1/72 英寸。 在 Linux? 系統上,一個像素的大小由系統分辨率確定。 |
‘Position’ — 文本位置(起始點)[ start_x , start_y ]
- 因為
'units'
設置為'normalized'
進行了歸一化【坐標區的左下角映射到 (0,0),右上角映射到 (1,1)。】
因此'position'
設定內值為gca(坐標區)的相對位置,所以start_x , start_y可以為“負”
或“超出坐標區(1,1)范圍”
。
HorizontalAlignment — 相對于位置點水平對齊文本
- 位置點為’Position’中設置的點位置
'left' (默認) | 'center' | 'right'
?
3. 舉個例子
x = linspace(-5,5);
y = x.^3-12*x;% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
% // 右圖
subplot(1,2,1)
plot(x,y)% // ===== Method 1 '藍色' - text(x,y,txt) =====text([-2 2],[16 -16],...['\bf\color[rgb]{0.24,0.35,0.67}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"會"\fontsize{12}變'])% // ===== Method 1 '紅色' =====================text('string',{['\bf\color[rgb]{0.69,0.09,0.12}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"不會"\fontsize{12}變'];'[對齊方式為默認left]'},...'Units','normalized',...'position',[-0.04,1.02]); % // 相對位置對齊方式為默認'left' text('string',{['\bf\color[rgb]{0.69,0.09,0.12}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"不會"\fontsize{12}變'];'[對齊方式設為right]'},...'Units','normalized',...'position',[1,0.8],...'HorizontalAlignment','right'); % // 相對位置對齊方式'靠右對齊'['right']% //%%%%% 改變X-Y坐標范圍 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% // 右圖
subplot(1,2,2)
% // ===== 設置新范圍 =====================
plot(x,y)
set(gca,'Xlim',[-2,5],'YLim',[-20,80])% // ===== Method 1 '藍色' - text(x,y,txt) =====text([-2 2],[16 -16],...['\bf\color[rgb]{0.24,0.35,0.67}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"會"\fontsize{12}變'])% // ===== Method 1 '紅色' =====================text('string',{['\bf\color[rgb]{0.69,0.09,0.12}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"不會"\fontsize{12}變'];'[對齊方式為默認left]'},...'Units','normalized',...'position',[-0.04,1.02]); % // 相對位置對齊方式為默認'left' text('string',{['\bf\color[rgb]{0.69,0.09,0.12}\fontsize{12}位置隨坐標范圍改動',...'\fontsize{18}"不會"\fontsize{12}變'];'[位置設為right]'},...'Units','normalized',...'position',[1,0.8],...'HorizontalAlignment','right'); % // 相對位置對齊方式'靠右對齊'['right']set(gcf,'position',[10 100 1000 330]);
set(gcf,'color','w');
//