文章目录
0、help
doc 函数%命令行输入doc plot可看到对应的函数用法
一、字体、线宽、颜色设置
clc;clear;close all;
color = linspecer(8);%按照图中线条数目设置,最好全文统一,函数在资源中可下载“matlab绘图颜色设置”
font = 10;%10pt
linewidth = 1.4;
二、文件读取与保存
x = 1520:0.2:1600;%1×401的double数组,横向量
x = linspace(1520,1600,401);%与上相同
y = readmatrix('data.xlsx','Sheet',1);%读取数组,matlab 2019a后开始有这个函数的
y = readmatrix('data.xlsx','Sheet',1).';%读取数组并转置
y = flipud(readmatrix('data.xlsx','Sheet',1));%从上向下翻转
saveas(gcf, 'a.emf');%保存为emf文件,矢量图
三、固定图片大小
figure(1)
set(gcf,'unit','centimeters','position',[10,5,12,9]);%(10,5)为图片在屏幕的起始坐标,不用管,12为图片宽度,论文一般要求为页面的80%即12cm,9为图片高度
set(gcf,'unit','centimeters','position',[10,5,6,4.5]);%准备横着放两张图片,宽度可以设置为6cm
set(gca,'Position', [.2 .2 .7 .7]);%绘图区域的比例设置:.2分别为左下角的起始位置占比,即(2.4cm,2.4cm);.7分别为绘图区域的占比
四、绘图
4.1 单坐标轴画线
plot(x,y,'LineWidth',linewidth,'Color',color(1,:));%画线
plot(x,y,'-o','LineWidth',linewidth,'Color',color(1,:));%画带圈的线,'-*','-^','-s'巴拉巴拉不同的线型
plot(x,y,'-o','MarkerIndices',i,'LineWidth',linewidth,'Color',color(1,:));%第i个点画圈,一般用来标记峰值,谷值等,i为对应下标
plot(maxF, maxAmp,'*', 'LineWidth',linewidth,'color',color(1,:),'HandleVisibility','off'); %图例中不显示此线
4.2 单坐标轴竖排两幅图
figure(1)
set(gcf,'unit','centimeters','position',[10,5,8,9]);
subplot(2,1,1);
set(gca,'xticklabel',[]);%不显示x坐标轴刻度
set(gca,'position',[.18 .55 .77 .43]);%上下大小一致,55-13=43
set(gca, 'FontSize',font,'FontName',fontname);
ax2=subplot(2,1,2);
set(gca,'position',[.18 .12 .77 .43]);
set(gca, 'FontSize',font,'FontName',fontname)
pos=axis(ax2);%取得最后的subpolt的坐标轴的范围,即[xmin xmax ymin ymax] [0 1 0 1]
xlabel('\fontname{宋体}空间频率\fontname{Times New Roman} \itf \rm(1/nm)','FontSize',font);
ylabel('\fontname{宋体}相位\fontname{Times New Roman} (π rad)','FontSize',font);
saveas(gcf, 'a.emf')
figure(2)
set(gcf,'unit','centimeters','position',[10,5,5.4,5.4]);
% 上一张
subplot(2,1,1);
plot(delta_nex,delta_neff,'o',delta_nex,ylinear,'LineWidth',linewidth,'Color',color(1,:));hold on;
set(gca,'xticklabel',[]);%不显示x坐标轴刻度
xlim([1.33,1.342]);
set(gca,'position',[.27 .58 .66 .34]);
set(gca, 'FontSize',font,'FontName',fontname)
lgd =legend('\fontname{宋体}原始数据','\fontname{宋体}拟合数据');
lgd.FontName = 'Times New Roman';
lgd.FontSize = font;
lgd.ItemTokenSize = [14,16];
set(legend,'Location','NorthEast');
legend boxoff;
% 下一张
ax2=subplot(2,1,2);
plot(delta_nex,delta_neff,'o',delta_nex,ylinear,'LineWidth',linewidth,'Color',color(2,:));
xlim([1.33,1.342]);
set(gca, 'XTick',1.33:0.004:1.342,'FontSize',font,'fontname','Cambria') % 设置x坐标轴的刻度
set(gca,'position',[.27 .23 .66 .34]);
set(gca, 'FontSize',font,'FontName',fontname)
pos=axis(ax2);%取得最后的subpolt的坐标轴的范围,即[xmin xmax ymin ymax] [0 1 0 1]
ylabel('\fontname{宋体}有效折射率差\fontname{Times New Roman} \it\Deltan_e_f_f\rm (RIU)','FontSize',font,'Position',[pos(1)-0.0031 pos(4)]);
xlabel('\fontname{宋体}待测分析物折射率 \fontname{Times New Roman}\itn_e_x\rm (RIU)','FontSize',font);
lgd =legend('\fontname{宋体}原始数据','\fontname{宋体}拟合数据');
lgd.FontSize = font;
lgd.FontName = 'Times New Roman';
lgd.ItemTokenSize = [14,16];
lgd.FontName = 'Times New Roman';
set(legend,'Location','NorthEast');
legend boxoff;
saveas(gcf, 'b.emf')
4.3 双坐标轴画线
yyaxis left;
plot(x,yl_1,'LineWidth',linewidth);
plot(x,yl_2,'LineWidth',linewidth);
yyaxis right;
plot(x,yr_1,'LineWidth',linewidth);
plot(x,yr_2,'LineWidth',linewidth);
4.4 热图
%% 原始数据
w_b = 760:10:810;
w_c = 0:30:750;
E0 = readmatrix('TE_fill_factor.xlsx').';
%% 插值
wb_after = linspace(min(w_b),max(w_b),5000);%wb插值数
wc_after = linspace(min(w_c),max(w_c),8000);%wc插值数
[X , Y ] = meshgrid(w_b ,w_c );
[X2, Y2] = meshgrid(wb_after ,wc_after);
E1 = interp2(X,Y,E0,X2,Y2,'spline');%E1:插值后电场,使用spline插值,图样平滑
E = E1;
colormap jet
imagesc(wb_after, wc_after, abs(E));%电场的模
colorbar;
caxis([0.03,0.04])
4.5 统计图
dataset=[169, 78;
141, 213];
% 绘制初始柱状图
GO = bar(dataset);
% 柱状图赋色
GO(1).FaceColor = color(7,:);
GO(2).FaceColor = color(13,:);
% X坐标轴刻度标签
set(gca,'Xticklabel',{'TE','TM'});
ylabel('\it S_{phase}\rm (π rad/RIU)');
lgd= legend([GO(1),GO(2)], ...
'BIMI', 'BIMI-MC', ...
'Location', 'northwest');
lgd.FontSize = font;
lgd.FontName = fontname;
legend boxoff;
4.6 带误差棒的统计图
% 读取数据
dataset=[169, 78; 141, 213];
% 误差矩阵
UP= [5, 8; 8, 6]; % 下方长度
DOWN= [9, 9; 10, 12]; % 上方长度
% 绘制初始柱状图
GO = bar(dataset);hold on;
% 柱状图赋色
GO(1).FaceColor = color(7,:);
GO(2).FaceColor = color(13,:);
% 添加误差棒
[M,N] = size(dataset);
xpos = zeros(M,N);
for i = 1:N
xpos(:,i) = GO(1,i).XEndPoints';
end
hE = errorbar(xpos, dataset, UP, DOWN);
% 误差棒属性
set(hE, 'LineStyle', 'none', 'Color', 'k', 'LineWidth', 1.2)
% X坐标轴刻度标签
set(gca,'Xticklabel',{'TE','TM'});
ylabel('\it S_{phase}\rm (π rad/RIU)');
lgd= legend([GO(1),GO(2)], ...
'BIMI', 'BIMI-MC', ...
'Location', 'northwest');
lgd.FontSize = font;
lgd.FontName = fontname;
legend boxoff;
4.7 带errorbar的折线图
errorbar(x,mean0-mean0(1),min0-mean0,max0-mean0,'o','LineWidth',linewidth,'Color',color(1,:));hold on;
cofficient1 = polyfit(x,mean0-mean0(1),1);
ylinear = polyval(cofficient1,x);
plot(x,ylinear,'LineWidth',linewidth,'Color',color(1,:),'HandleVisibility','off');hold on;
五、坐标轴的label(怎么同时表示中文与英文两种格式)
两种格式:文字前标明格式 \fontname{宋体}我爱学习,学习使我快乐
\fontname{Times New Roman}I love study and study makes me happy.
斜体:\it我是斜体\rm我正了
下标:w_b我是一级下标;w_s_i=w_{si}我是连着的;w_{sio_2}我是下标的下标
希腊字符:https://blog.csdn.net/GarfieldEr007/article/details/51510805
xlabel('\fontname{宋体}双模波导宽度\fontname{Times New Roman} \itw\rm_b (nm)','FontSize',font);
ylabel('\fontname{Times New Roman}SiO_2\fontname{宋体}宽度\fontname{Times New Roman} \itw\rm_c (nm)','FontSize',font);
set(gca,'YDir','normal');%正向显示;'rev'逆向显示
set(gca, 'XTick',760:10:810,'FontSize',font,'fontname','Times New Roman') % 设置x坐标轴的刻度
set(gca, 'YTick',0:150:750,'FontSize',font) % 设置y坐标轴的刻度
六、图例设置legend
lgd = legend('TE BIMI',...
'TM BIMI',...
'TE BIMI-MC',...
'TM BIMI-MC');%字体设置与第五点相同\fontname{Times New Roman}SiO_2
lgd.FontSize = font;%字体大小
lgd.FontName = 'Times New Roman';%字体类型
lgd.ItemTokenSize = [12,14];%图例的长度
set(legend,'Location','NorthEast');位置
legend boxoff;%关闭那个黑框框