Difficulties having log and linear y axes on same figure

22 visualizaciones (últimos 30 días)
Herbert Middleton
Herbert Middleton el 18 de Jun. de 2022
Editada: Voss el 19 de Jun. de 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}

Respuesta aceptada

Star Strider
Star Strider el 18 de Jun. de 2022
Editada: Star Strider el 18 de Jun. de 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 comentarios
Herbert Middleton
Herbert Middleton el 18 de Jun. de 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 el 18 de Jun. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 18 de Jun. de 2022
Editada: Voss el 18 de Jun. de 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 comentarios
Voss
Voss el 19 de Jun. de 2022
You're welcome!

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by