MATLAB常见图形格式调整问题
1 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]); % 给坐标加标签
set(gca,'XMinorTick','on'); % 次刻度线
set(gca,'XTickLabelRotation',30); % 刻度标签旋转
set(gca,'TickDir','in' ); % 刻度线方向
% 设置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 线型设置
线型 | 说明 |
---|---|
- | 实线 |
– | 虚线 |
: | 点线 |
-. | 点划线 |
MATLAB可以更改下列属性:
· LineWidth - 指定线条的宽度(以磅为单位)。
· MarkerEdgeColor - 指定标记颜色或填充标记(圆形、方形、菱形、五角形、六角形和四个三角形)的· 边颜色。
· MarkerFaceColor - 指定填充标记的面的颜色。
· MarkerSize - 指定标记的大小(以磅为单位,必须大于 0)。
5 MATLAB分区绘图subplot总标题设置
分区绘图语法:
subplot(m,n,p)
subplot(m n p)
- m表示图排成m行
- n表示图排成n列
- p表示图所在的位置,p=1表示从左到右从上到下的第一个位置
在代码末处添加以下代码,可以设置总标题:
属性修改可见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
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', []);
11 图片尺寸设置(单位:厘米)
figureUnits = 'centimeters';
figureWidth = 18;
figureHeight = 12;
figureHandle = figure;
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
hold on
12 MATLAB中文设置宋体,西文设置新罗马字体(Times New Roman)
设置中文宋体,西文新罗马字体代码如下:
hxlabel = text(-0.1,0.5,'\fontname{宋体}\fontsize{15}面积\fontname{Times New Roman}\fontsize{15} /10^4hm^2)');
set(hxlabel,'fontsize',15,'rotation',90,'HorizontalAlignment','center','FontWeight','Bold');
hylabel = text(0.525,-0.1,'\fontname{宋体}\fontsize{15}年份');
set(hylabel,'fontsize',15,'HorizontalAlignment','center','FontWeight','Bold');
案例示意图如下:
13 设置坐标轴位置
1、设置X轴位置
set(gca,'XAxisLocation','left'); % 将x轴的位置设置在左边(默认)
set(gca,'XAxisLocation','right'); % 将x轴的位置设置在右边
set(gca,'XAxisLocation','origin'); %将x轴的位置设置在y=0处
2、设置Y轴位置
set(gca,'YAxisLocation','left'); % 将y轴的位置设置在左边(默认)
set(gca,'YAxisLocation','right'); % 将y轴的位置设置在右边
set(gca,'YAxisLocation','origin'); %将y轴的位置设置在x=0处
参考
1-CSDN博客-Matlab去除图像右边和上边的刻度线
2.知乎-MATLAB绘制SCI插图系列-1气泡图
3.Matlab坐标轴标签中文设置宋体,英文设置新罗马字体(Times New Roman)