Difficulties having log and linear y axes on same figure

22 次查看(过去 30 天)
I am trying to plot a graph that has: left y axis in a logarithmic scale and a right y axis in a linear scale...for some reason I am not able to do it. (The x axis is always logarithmic in this case).
Any help is appreciated, as I can't seem to get my head around what is wrong:
figure(1)
FontSizeCaption = 18;
FontSizeLabel = 16;
LineWidth= 1.5;
loglog(CSS_NF_1,EM_NF_1,'-rsq','LineWidth',LineWidth)
hold on
loglog(CSS_NF_1,VM_NF_1,'-bo','LineWidth',LineWidth)
caption = sprintf('Linear Viscoelastic Region Analysis \n (KHAp)');
title(caption, 'FontSize', FontSizeCaption,'FontName','CMU Sans Serif');
xlbl_N=xlabel('Complex Shear Strain (%)');
set(xlbl_N,'FontSize',FontSizeLabel,'FontName','CMU Sans Serif');
yyaxis left
ylbll_N=ylabel('Elastic Modulus, Viscous Modulus(Pa)');
set(ylbll_N,'FontSize',16,'FontName','CMU Sans Serif');
yyaxis right
semilogx(CSS_NF_1,PA_NF_1,'-^','LineWidth',LineWidth,'Color','#377E22')
ylblr_N=ylabel('Phase Angle (^{\circ})')
ylim([0 90])
set(ylblr_N,'FontSize',FontSizeLabel,'FontName','CMU Sans Serif','Color','#377E22');
ax = gca;
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = '#377E22';
Side point: I also can't seem to separate my y labels in lines by using {\n}

采纳的回答

Star Strider
Star Strider 2022-6-18
编辑:Star Strider 2022-6-18
The data are missing so I can’t run the posted code.
Try something like this —
x = linspace(0,100);
y1 = 5.1+5*sin(2*pi*x*3);
y2 = 2.1+2*cos(2*pi*x*3);
figure
yyaxis left
plot(x, y1)
ylabel('Log Scale')
yyaxis right
plot(x, y2)
ylabel('Linear Scale')
Ax = gca;
% Ax.YAxis(1)
% Ax.YAxis(2)
Ax.YAxis(1).Scale = 'log';
Ax.YAxis(2).Scale = 'linear';
Setting the axis properties after the plots are created is straightforward. See Modify Properties of Charts with Two y-Axes.
.

更多回答(1 个)

Voss
Voss 2022-6-18
编辑:Voss 2022-6-18
This is your code, with some random data. It seems to do the right thing, as far as the axes scales (linear/log) being correct.
To get the ylabel on multiple lines, use sprintf with \n (like you are already doing with the title).
CSS_NF_1 = logspace(-1,2,25);
EM_NF_1 = rand(size(CSS_NF_1));
VM_NF_1 = rand(size(CSS_NF_1));
PA_NF_1 = 90*rand(size(CSS_NF_1));
figure(1)
FontSizeCaption = 18;
FontSizeLabel = 16;
LineWidth= 1.5;
loglog(CSS_NF_1,EM_NF_1,'-rsq','LineWidth',LineWidth)
hold on
loglog(CSS_NF_1,VM_NF_1,'-bo','LineWidth',LineWidth)
caption = sprintf('Linear Viscoelastic Region Analysis \n (KHAp)');
title(caption, 'FontSize', FontSizeCaption,'FontName','CMU Sans Serif');
xlbl_N=xlabel('Complex Shear Strain (%)');
set(xlbl_N,'FontSize',FontSizeLabel,'FontName','CMU Sans Serif');
yyaxis left
ylbll_N=ylabel(sprintf('Elastic Modulus\nViscous Modulus\n(Pa)'));
set(ylbll_N,'FontSize',16,'FontName','CMU Sans Serif');
yyaxis right
semilogx(CSS_NF_1,PA_NF_1,'-^','LineWidth',LineWidth,'Color','#377E22')
ylblr_N=ylabel('Phase Angle (\circ)');
ylim([0 90])
set(ylblr_N,'FontSize',FontSizeLabel,'FontName','CMU Sans Serif','Color','#377E22');
ax = gca;
ax.YAxis(1).Color = 'k';
ax.YAxis(2).Color = '#377E22';
  4 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by