Difficulties having log and linear y axes on same figure

24 ビュー (過去 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 件のコメント
Herbert Middleton
Herbert Middleton 2022 年 6 月 19 日
Amazing, thanks!
Voss
Voss 2022 年 6 月 19 日
You're welcome!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by