Difficulties having log and linear y axes on same figure

24 vues (au cours des 30 derniers jours)
Herbert Middleton
Herbert Middleton le 18 Juin 2022
Modifié(e) : Voss le 19 Juin 2022
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}

Réponse acceptée

Star Strider
Star Strider le 18 Juin 2022
Modifié(e) : Star Strider le 18 Juin 2022
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 commentaires
Herbert Middleton
Herbert Middleton le 18 Juin 2022
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 le 18 Juin 2022
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 18 Juin 2022
Modifié(e) : Voss le 18 Juin 2022
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 commentaires
Herbert Middleton
Herbert Middleton le 19 Juin 2022
Amazing, thanks!
Voss
Voss le 19 Juin 2022
You're welcome!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by