使用MATLAB画图,为了获得高质量的画图结果,常需要用绘图语句(部分语句来自网络,仅供学习)进行指定。这些语句一般是固定的,在实际绘图可以按需修改,不必每次都重复设定。
一. figure gcf gca 等
figure
设定figure的name(名字,在产生许多图时,可以用str来区别),color背景颜色(一般取白色W),
图窗大小和位置unit和position(个人习惯用centimeter)
figure('name','污染物','color','w',...
'unit','centimeter','position',[9 9 25 20]);
or
figure(99) % 随意新建一个figure,再用鼠标手动拉伸到合适的大小,这时unit选项为默认值
pos99 = get(gcf,'position'); % 获取当前满意figure的位置
set(gcf,'position',pos99); % 设置当前满意figure的位置
figure(100) % 再新建一个figure
set(gcf,'position',pos99); % 设置当前满意figure的位置
这样做可以统一所有输出figure的大小和位置
gca
坐标轴标注,时间序列曲线(如温度的时间变化曲线)一般要在特定的时刻标注出对应的时间标(如一年8760小时中1月1日,2月1日,…, 12月1日),要首先找到XTick在序列中的位置。
set(gca,'XTick',[ 32 60 91 121 152 182 213 244 274 305 335])
set(gca,'xticklabel',{'02-01','03-01','04-01','05-01','06-01','07-01','08-01','09-01','10-01','11-01','12-01'})%x轴刻度
set(gca,'fontsize',15,'fontweight','bold');
坐标轴字体大小与黑体(一般学术论文用的多,MATLAB自带的字体太细,导出以后看不清楚)
set(gca,'fontsize',15,'fontweight','bold');
二. 绘图函数 plot contour
contour
cmap=flip(colormore_66(100));
hh=figure;
contourf(x,z,temp_x_z,100,'linestyle','none')
hold on
grid on
[c,h]=contour(x,z,temp_x_z,[00:2:30]);%12到30每隔两个画一条等值线;
set(h,'ShowText','on','color','white','linewidth',2,'Fill', 'on');%显示数值
clabel(c,h,'fontsize',12)
caxis([0 32])
colorbar
colormap(cmap)
set(gca,'linewidth',2,'fontsize',20,'fontweight','bold')
xtickformat('%d °E')
set(gcf,'position',[50,50,1200,1000])
title('温度剖面图','fontsize',20,'fontweight','bold')
export_fig(hh,'温度剖面图3.png','-r600')
[c,h]=contour(x,z,temp_x_z,[00:2:30]);%12到30每隔两个画一条等值线;
set(h,'ShowText','on','color','white','linewidth',2,'Fill', 'on');%显示数值
clabel(c,h,'fontsize',12)
caxis([0 32])
colorbar
colormap(cmap)
三. 绘图辅助函数(title legend text)等 上下角标用法 ^{superstring}——进行上标文本的注释 _{substring}——进行下标文本的注释 用{}把须放在一起的括起来
...画图代码
legend({'A_{M_2}','A_{M_4}','R_{{M_4}/{M_2}}'})
https://jingyan.baidu.com/article/fec7a1e51b2d4f1190b4e7ff.html
title
legend
legend在多条曲线时(特别包括数学公式时),一般要指定interpreter为latex选项。lgdstr太长,单独设定一个变量lgdstr(可访问latex在线网站https://www.latexlive.com/ latex在线),还可以指定legend的字体大小fontsize和黑体
lgdstr = {'(10,0)';...
'$(10\cos(2\pi\cdot t/24),10\sin(2\pi\cdot t/24))$';...
'$(10+5\cos(2\pi\cdot t/24),0)$';...
'$((10+5\sin(2\pi\cdot t/24))\cos(2\pi\cdot t/24),(10+5\sin(2\pi\cdot t/24))\sin(2\pi\cdot t/24))$'}
lgd = legend(lgdstr,'fontsize',12,'fontweight','bold','interpreter','latex');
lgdstr2 = {'z = -1';'z = -21';'z = -41';...
'z = -61';'z = -81';'z = -99';};
lgd2 = legend(lgdstr2,'fontsize',12,'fontweight','bold','interpreter','latex');
legend({'$$','$$',' ',' ',' ',' ',' ',' ',' '},...
'fontsize',12,'fontweight','bold',...
'Orientation','horizontal','location','southoutside',...
'interpreter','latex')
上面的命令是将后7个绘图命令的标注设置为空,legend不显示
Matlab plot多条曲线使用legend标注某几条图例 用句柄指定
x = 0 : 0.01 : 4*pi;
y1 = sin(x);
y2 = cos(x);
y3 = 3*cos(x);
y4 = cos(x).*sin(x);
y5 = cos(x) + sin(x);
y6 = cos(x) - 2*sin(x);
h1 = plot(x,y1,x,y2,x,y3);
hold on
h2 = plot(x,y4,x,y5,x,y6);
legend([h1(1),h1(3),h2(2)],'sin(x)','3cos(x)','cos(x) + sin(x)') % 只标注对应曲线 其他曲线不显示
https://jingyan.baidu.com/article/4b52d702e0c865fc5d774b54.html