生活随笔
收集整理的這篇文章主要介紹了
【合集】MATLAB常见图形格式调整问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MATLAB常見圖形格式調整問題
- 1 MATLAB固定text在圖中的相對位置
- 2 MATLAB繪制圖像時調整坐標軸及網格線至最頂層
- 3 MATLAB設置坐標范圍:X/Y軸
- 4 MATLAB 線型設置
- 5 MATLAB分區繪圖subplot總標題設置
- 6 MATLAB分區繪圖子圖間距和邊緣距離調整
- 7 MATLAB圖例設置Legend
- 8 MATLAB設置斜體和正體
- 9 colormap相關問題
- 10 坐標軸刻度設置
- 10.1 坐標軸刻度朝外
- 10.2 去除圖像右邊和上邊的刻度線
- 10.3
- 參考
1 MATLAB固定text在圖中的相對位置
MATLAB如何固定text在圖中的相對位置
1.1 text函數
語法:
text
(x,y,txt
)
text
(___,Name,Value
)
text的詳細用法參見MATLAB幫助文檔
一般text函數的前面兩項是輸入插入string的坐標位置,但若想固定string在圖中的相對位置,比如說讓string 一直顯示在圖的右上角,并不隨圖的坐標軸的大小的改變而改變,那這個應當如何實現呢?
利用text的Unit屬性,利用normalized的單位來實現
實例如下:
text
( 'string',
"(a) UB-OR",
'Units',
'normalized',
'position',
[0.75,0.95
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
據此,固定了text在圖中的相對位置,在繪制一系列圖表時,可移植至其他圖形。
2 MATLAB繪制圖像時調整坐標軸及網格線至最頂層
MATLAB繪制圖像時調整坐標軸及網格線至最頂層
MATLAB在繪制圖形時,會出現圖像遮擋坐標軸,網格線等情況,此時,如何調整圖層的先后順序呢?
于末尾添加代碼如下:
set
(gca,
'Layer',
'top');
經修改后,圖形如下所示,可見問題已經解決~
3 MATLAB設置坐標范圍:X/Y軸
% 設置x軸范圍和刻度
set
(gca,
'XLim',
[0 10]); % X軸的數據顯示范圍
set
(gca,
'XTick',
[0:1:10
]); % 設置要顯示坐標刻度
set
(gca,
'XTickLabel',
[0:1:10
]); % 給坐標加標簽 % 設置y軸范圍和刻度
set
(gca,
'YLim',
[95 101]); % X軸的數據顯示范圍
set
(gca,
'YTick',
[95:1:101
]); % 設置要顯示坐標刻度
set
(gca,
'YTickLabel',
[95:1:101
]); % 給坐標加標簽 % 設置當前坐標軸x軸和y軸的限制范圍
axis
( [xmin xmax ymin ymax
] )
4 MATLAB 線型設置
參見LineSpec(線條設定)
- | 實線
– |虛線
: | 點線
-. | 點劃線
MATLAB可以更改下列屬性:
· LineWidth - 指定線條的寬度(以磅為單位)。
· MarkerEdgeColor - 指定標記顏色或填充標記(圓形、方形、菱形、五角形、六角形和四個三角形)的· 邊顏色。
· MarkerFaceColor - 指定填充標記的面的顏色。
· MarkerSize - 指定標記的大小(以磅為單位,必須大于 0)。
5 MATLAB分區繪圖subplot總標題設置
在代碼末處添加以下代碼,可以設置總標題:
屬性修改可見MATLAB幫助-在子圖網格上添加標題
sgtitle
('Your Title');
對于共用橫(縱)坐標標題,可以通過text函數實現:
hAxis
= axes
('visible',
'off');
hxlabel
= text
(-0.05,0.5,
'Percentage(%)');
set
(hxlabel,
'fontsize',15,
'rotation',90,
'HorizontalAlignment',
'center',
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
hylabel
= text
(0.5,-0.1,
'Month');
set
(hylabel,
'fontsize',15,
'HorizontalAlignment',
'center',
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
效果圖如下:
6 MATLAB分區繪圖子圖間距和邊緣距離調整
在MATLAB中,利用subplot繪制多副子圖時,會發現各子圖之間的距離太遠,如下圖所示:
So,怎么調整呢?
先百度,首先嘗試Matlab子圖間距和邊緣距離調整一文中提出的兩個方法。
方法一:使用tight_subplot函數,替代subplot進行繪圖設置。
下載地址:tight_subplot(Nh, Nw, gap, marg_h, marg_w)
函數調用格式:
[ha,pos
]=tight_subplot
(Nh,Nw,gap,marg_h,marg_w
)
% ha 是坐標軸句柄,pos是每個坐標軸的原點與長寬
% Nh,Nw 可以認為是幾行幾列
% gap是子圖的縱向和橫向間距,gap
(1)為縱向,gap
(2)為橫向
% marg_h是圖件與上下邊緣的距離,marg_h
(1)為距下邊緣的距離,marg_h
(2)是距上邊緣的距離
% marg_w 是圖件與左右邊緣的距離,marg_w
(1)為距左邊緣的距離,marg_w
(2)是距右邊緣的距離。
利用上述函數進行處理,得到圖形如下:
相關代碼如下:
figure
(1)
Nh
= 4; % 兩行
Nw
= 2; % 四列
ha
= tight_subplot
(Nh,Nw,
[.01 .01
],
[.1 .01
],
[.05 .01
]);axes
( ha
(1) ); % 將h(a)設置為當前坐標區
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{1,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{1,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{1,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(a) Huangjiagang",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[ ]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[-1
0 1 2 3]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(2) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{2,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{2,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{2,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(b) Huangzhuang",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[ ]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[ ]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(3) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{3,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{3,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{3,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(c) Baihe",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[ ]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[-1
0 1 2 3]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(4) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{4,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{4,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{4,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(d) Guotan",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[ ]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[ ]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(5) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{5,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{5,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{5,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(e) Huanglongtan",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[ ]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[-1
0 1 2 3]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(6) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{6,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{6,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{6,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(f)Xiangjiaping",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[1958:5:2013
]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[ ]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(7) );
hold on
;
box on
;
h
(1) = plot
(SRI3Spring
{7,1},
'-ob',
'LineWidth',1.5,
'MarkerEdgeColor',
'b');
h
(2) = plot
(SRI3Summer
{7,1},
'-sr',
'LineWidth',1.5,
'MarkerEdgeColor',
'r');
h
(3) = plot
(SRI3Autumn
{7,1},
'-g',
'LineWidth',1.5
);
h
(4) = plot
([0,57],
[0,0],
'--k',
'LineWidth',1
);
text
( 'string',
"(g)Xindianpu",
'Units',
'normalized',
'position',
[0.02,0.9
],
'FontSize',14,
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
set
(gca,
'xlim',
[0 57],
'xtick',
[1:5:57
],
'xticklabel',
[1958:5:2013
]);
set
(gca,
'ylim',
[-2
3.25],
'ytick',
[-1:1:3
],
'yticklabel',
[-1
0 1 2 3]);
set
(gca,
'FontSize',12,
'Fontname',
'Times New Roman');axes
( ha
(8) );
axis off
;hAxis
= axes
('visible',
'off');
hxlabel
= text
(0.5,-0.1,
'Year');
set
(hxlabel,
'fontsize',15,
'HorizontalAlignment',
'center',
'FontWeight',
'Bold',
'FontName',
'Times New Roman');
hylabel
= text
(-0.14,0.5,
'SRI');
set
(hylabel,
'fontsize',15,
'rotation',90,
'HorizontalAlignment',
'center',
'FontWeight',
'Bold',
'FontName',
'Times New Roman');lgd
= legend
(h
([1 2 3]),
"Spring",
"Summer",
"Autumn",
'fontsize',15,
'FontWeight',
'Bold',
'FontName',
'Times New Roman',
'NumColumns',3
);
set
(lgd,
'Box',
'off');
此外,針對多余坐標區(上圖第八個分區沒有圖,用于放置圖例),可用以下代碼將其隱藏。
axes
( ha
(8) );
axis off
;
方法二:針對2019b以后的版本,官網有tiledlayout-創建分塊圖布局可以用于解決間距過大的問題。
沒辦法,我現在也是2019a,沒得使用這個方法的機會。
7 MATLAB圖例設置Legend
參見MATLAB幫助-Legend在坐標區上添加圖例
7.1 圖例位置
值說明
| ‘north’ | 坐標區中的頂部 |
| ‘south’ | 坐標區中的底部 |
| ‘east’ | 坐標區中的右側區域 |
| ‘west’ | 坐標區中的左側區域 |
| ‘northeast’ | 坐標區中的右上角(二維坐標區的默認值) |
| ‘northwest’ | 坐標區中的左上角 |
| ‘southeast’ | 坐標區中的右下角 |
| ‘southwest’ | 坐標區中的左下角 |
| ‘northoutside’ | 坐標區的上方 |
| ‘southoutside’ | 坐標區的下方 |
| ‘eastoutside’ | 到坐標區的右側 |
| ‘westoutside’ | 到坐標區的左側 |
| … | … |
示例: legend(‘Location’,‘northeastoutside’)
7.2 其它
設置列數: lgd.NumColumns = 3
7.3 案例
MATLAB實現代碼如下:
hl
= legend
(h
([1 2]),
"徑流",
"多年平均徑流",
'location',
'northwest');
set
(hl,
'Box',
'off');
圖形如下:
8 MATLAB設置斜體和正體
斜體(Italic)
\it
正體
\rm
案例如下:
相應代碼如下:
figure
(1)
box on
;
backColor
= [255 240 245]/255
;
set
(gca,
'color', backColor
);
text
( 'string',
"\itZ\rm_1=\itz\rm_1+\itz\rm_2+\itz\rm_3",
'Units',
'normalized',
'position',
[0.5,0.8
],
'FontSize',25,
'FontWeight',
'Bold',
'FontName',
'Times New Roman',
'color',
'r');
9 colormap相關問題
MATLAB中文幫助-colormap
MATLAB中文幫助-colorbar
MATLAB預定義的顏色圖:
9.1 顏色圖反轉
【舉例】將灰度圖顏色反轉,即白色部分變黑,黑色部分變白:
colormap
(flipud
(gray
)):
10 坐標軸刻度設置
10.1 坐標軸刻度朝外
set
(gca,
'TickDir',
'out')
疑問:此方法只能設置將各防線坐標軸刻度朝外,如何設置某單個坐標軸刻度方向呢?
10.2 去除圖像右邊和上邊的刻度線
box off
ax2
= axes
('Position',get
(gca,
'Position'),
...
'Color',
'none',
...
'XAxisLocation',
'top',
...
'YAxisLocation',
'right',
...
'XColor',
'k',
'YColor',
'k');
set
(ax2,
'YTick',
[]);
set
(ax2,
'XTick',
[]);
10.3
參考
1-CSDN博客-Matlab去除圖像右邊和上邊的刻度線
總結
以上是生活随笔為你收集整理的【合集】MATLAB常见图形格式调整问题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。