Matlab-多y轴图片绘制

发布时间 2023-10-03 16:39:43作者: Drizzly_n
%% 多Y轴图

%% Made by Lwcah in 2023-06-26(公众号:Lwcah)
%% 公众号:Lwcah
%% 知乎、B站、小红书、抖音同名账号:Lwcah,感谢关注~
%% 更多MATLAB+SCI绘图教程敬请观看~

%% 清除环境变量
close all;clear all;clc;

%% 1行1列-定义整幅图出现的在电脑屏幕上的位置以及长和宽
figureHandle = figure;
figureUnits = 'centimeters';
figureWidth = 15;
figureHeight = 8;
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);

%% 定义字体和字号大小-通常的SCI一般Arial字体和10字号通用
fontnamed='Arial'; % 字号名字Arial
ssize=10;            % 字号大小
% 如果是中文论文可以相应的更改字号名字如下
% '华文中宋' '华文仿宋' '华文宋体' '华文新魏' '华文楷体'
% '华文琥珀' '华文细黑' '华文行楷' '华文隶书' '宋体'
% '方正姚体' '微软雅黑' '方正舒体' '新宋体'
% '幼圆' '楷体' '等线' '隶书' '黑体'

%% 给定绘图颜色
C1 = chinesecolors(343); % 香水玫瑰
C2 = chinesecolors(150); % 靛青
C3 = chinesecolors(523); % 玫瑰灰
C4 = chinesecolors(232); % 粉绿

%%
% handle = maxis(number of axis, y-spacing between outside lines)
h = myaxisc(4,0.10); % 第一个参数4是设置轴的数量,第二个参数0.10是设置轴间距
% Create some random data for plotting
t1  = 0:0.1:5;
t2  = 0:1:5;
y11 = sin(t1);
y21 = t1.^2-5;
y22 = 15-t2.*2;
y31 = sqrt(t1).*2+97;
y41 = rand(size(t1))-2;
y42 = rand(size(t1))+4;

p(1) = plot(h.p(1),t1,y11,'Color',C1);hold on;
p(2) = plot(h.p(2),t1,y21,'Color',C2,'LineStyle','--','Marker','o');hold on;
p(3) = plot(h.p(2),t2,y22,'Color',C2,'LineStyle','--','Marker','s');hold on;
p(4) = plot(h.p(3),t1,y31,'Color',C3);hold on;
p(5) = plot(h.p(4),t1,y41,'Color',C4,'LineStyle','--','Marker','o');hold on;
p(6) = plot(h.p(4),t1,y42,'Color',C4,'LineStyle','--','Marker','s');hold on;
% p(7) = bar(h.p(4),t1,y42,0.20,'FaceColor',C4);hold on; % 如果要画柱状图的话

h.xlim([0,5]);                       % Set X-Axis Limits
h.autoscale;                         % Automatically Scale Y Axis
h.autoy(3);                          % Autoscale only specified y-axis
% h.ylim(3,[95,105]);                % Set Y-Limits for axis 3
% h.ylim(4,[-3,8]);                  % Set Y-Limits for axis 4
h.gridon;                            % Enable grid (use h.gridoff to remove)         
h.ycolor(1,C1);                      % Modify the y-Axis Color
h.ycolor(2,C2);                      % Modify the y-Axis Color
h.ycolor(3,C3);                      % Modify the y-Axis Color
h.ycolor(4,C4);                      % Modify the y-Axis Color
h.ylabel(1,'First Y-Axis (Y1)');     % Add y-Labels
h.ylabel(2,'Second Y-Axis (Y2)');    % Add y-Labels
h.ylabel(3,'Third Y-Axis (Y3)');     % Add y-Labels
h.ylabel(4,'Another Y-Axis(Y4)');    % Add y-Labels
h.xlabel('X-Axis');                  % Add x-Label
h.fontsize(10);                      % Change all font sizes
h.position([0.1,0.15,0.8,0.75],0.12); % Position-Vector and Spacing 0.12

%% 增添图例
kk=legend(h.legendtarget,p,'Line 1','Line 2','Line 3','Line 4','Line 5','Line 6');
set(kk,'location','NorthOutside','Box', 'off','Orientation','horizontal','fontsize',10,'FontName',fontnamed);
% Matlab中有许多位置可以选择:
% 'North' inside plot box near top
% 'South' inside bottom
% 'East' inside right
% 'West' inside left
% 'NorthEast' inside top right (default for 2-D plots)
% 'NorthWest' inside top left
% 'SouthEast' inside bottom right
% 'SouthWest' inside bottom left
% 'NorthOutside' outside plot box near top
% 'SouthOutside' outside bottom
% 'EastOutside' outside right
% 'WestOutside' outside left
% 'NorthEastOutside' outside top right (default for 3-D plots)
% 'NorthWestOutside' outside top left
% 'SouthEastOutside' outside bottom right
% 'SouthWestOutside' outside bottom left
% 'Best' least conflict with data in plot 与绘图中的数据冲突最小
% 'BestOutside' least unused space outside plot

%% 背景颜色
set(gcf,'Color',[1 1 1])
%% 设置完毕后,按照所需分辨率、格式输出
figW = figureWidth;
figH = figureHeight;
set(figureHandle,'PaperUnits',figureUnits);
set(figureHandle,'PaperPosition',[0 0 figW figH]);
fileout = 'myaxisc_example';
print(figureHandle,[fileout,'.png'],'-r800','-dpng');