【matlab】多坐标轴的纵坐标刻度颜色的更改
我们正常不改颜色就是两边都是各自和线条颜色一致。
但我想要ylabel和yticklabel都是黑色或其它,比如下图:
该如何调整呢,我目前虽然能改,但没找到简便的语句,自己写了能用但
能用就好。
ylabel可以直接设置颜色:
ylabel('Netflux','color','k')
但yticklabel没能找到这样的语句。
我就人为干扰,
直接使用覆盖法:即自己重新给写一遍yticklabel覆盖,使用自己想使用的颜色。
% yticklabel_left
yticklabel =50:5:80;
for i=1:length(yticklabel)
text(1997.2,yticklabel(i),num2str(yticklabel(i)),'fontname','new times roman','fontsize',12,'FontWeight','bold','Color','k');
end
% yticklabel_right
yticklabel =0.65:0.05:1;
for i=1:length(yticklabel)
text(2022.2,yticklabel(i),num2str(yticklabel(i)),'fontname','new times roman','fontsize',12,'FontWeight','bold','Color','k');
end
至此,完成!如上图:
以下全部代码:
clear;clc;close all;
% load data
load("data.mat");
% 双坐标轴的颜色
mycolors1 =[65 105 225]./255;
mycolors2 =[247 51 51]./255;
% ---01---未改变双坐标轴刻度的颜色,即于线条颜色一致
close all
figure
set(gcf,'Position',[50 50 900 600],'Color','w')
yyaxis left
h1=plot(data_x,data_y1,'-o','LineWidth',1.5,'Color',mycolors1,'MarkerSize',6,'MarkerEdgeColor',mycolors1,'MarkerFaceColor',mycolors1);
hold on
set(gca,'ycolor',mycolors1,'xtick',1998:2:2022,'XTickLabel',1998:2:2022,'fontname','new times roman','fontsize',10,'FontWeight','bold','linewidth',2)
hold on
ylabel('SST','fontname','new times roman','fontsize',12,'FontWeight','bold');
yyaxis right
h2=plot(data_x,data_y2,'-s','LineWidth',1.5,'Color',mycolors2,'MarkerSize',6,'MarkerEdgeColor',mycolors2,'MarkerFaceColor',mycolors2);
hold on
% 设置网格线
grid on
ax = gca;
ax.GridLineStyle = '--';
ax.GridColor = [0.5,0.5,0.5];
ax.GridAlpha = 0.5;
ax.GridLineWidth = 0.5; % 设置透明度
set(gca,'ycolor',mycolors2,'xtick',1998:2:2022,'XTickLabel',1998:2:2022,'fontname','new times roman','fontsize',12,'FontWeight','bold','linewidth',2)
ylabel('Netflux','fontname','new times roman','fontsize',12,'FontWeight','bold')
xlim([1998 2022])
leng=legend([h1,h2],'SST','Net_flux','fontname','new times roman','fontsize',12,'FontWeight','bold');
set(leng,'Position',[0.14 0.845 0.23 0.07],'box','off')
export_fig('not_change_axis_change_yticklabel.jpg','-r600')
%--02--- 改变双坐标轴刻度的颜色,即不于线条颜色一致
close all
figure
set(gcf,'Position',[50 50 900 600],'Color','w')
yyaxis left
h1=plot(data_x,data_y1,'-o','LineWidth',1.5,'Color',mycolors1,'MarkerSize',6,'MarkerEdgeColor',mycolors1,'MarkerFaceColor',mycolors1);
hold on
set(gca,'ycolor',mycolors1,'xtick',1998:2:2022,'XTickLabel',1998:2:2022,'fontname','new times roman','fontsize',10,'FontWeight','bold','linewidth',2)
hold on
% yticklabel
yticklabel =50:5:80;
for i=1:length(yticklabel)
text(1997.2,yticklabel(i),num2str(yticklabel(i)),'fontname','new times roman','fontsize',12,'FontWeight','bold','Color','k');
end
ylabel('SST','fontname','new times roman','fontsize',12,'FontWeight','bold','Color','k');
yyaxis right
h2=plot(data_x,data_y2,'-s','LineWidth',1.5,'Color',mycolors2,'MarkerSize',6,'MarkerEdgeColor',mycolors2,'MarkerFaceColor',mycolors2);
hold on
% 设置网格线
grid on
ax = gca;
ax.GridLineStyle = '--';
ax.GridColor = [0.5,0.5,0.5];
ax.GridAlpha = 0.5;
ax.GridLineWidth = 0.5; % 设置透明度
% yticklabel
yticklabel =0.65:0.05:1;
for i=1:length(yticklabel)
text(2022.2,yticklabel(i),num2str(yticklabel(i)),'fontname','new times roman','fontsize',12,'FontWeight','bold','Color','k');
end
set(gca,'ycolor',mycolors2,'xtick',1998:2:2022,'XTickLabel',1998:2:2022,'fontname','new times roman','fontsize',12,'FontWeight','bold','linewidth',2,'ytick',yticklabel,'YTickLabel',yticklabel)
ylabel('Netflux','fontname','new times roman','fontsize',12,'FontWeight','bold','color','k')
xlim([1998 2022])
ylim([yticklabel(1) yticklabel(end)]);
leng=legend([h1,h2],'SST','Net_flux','fontname','new times roman','fontsize',12,'FontWeight','bold');
set(leng,'Position',[0.14 0.845 0.23 0.07],'box','off')
export_fig('change_axis_change_yticklabel.jpg','-r600')