Difficulties having log and linear y axes on same figure

조회 수: 14 (최근 30일)
Herbert Middleton
Herbert Middleton 2022년 6월 18일
편집: Voss 2022년 6월 19일
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.
.
  댓글 수: 2
Herbert Middleton
Herbert Middleton 2022년 6월 18일
Figured out that the problem was my data set (not large enough)...so I defined the ylim to allow for a log scale. Thanks!
Star Strider
Star Strider 2022년 6월 18일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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
Voss
Voss 2022년 6월 19일
You're welcome!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by