How can I have a graph with two logarithmic y axes?

16 visualizaciones (últimos 30 días)
Pim Wijnands
Pim Wijnands el 26 de Oct. de 2017
Comentada: Pim Wijnands el 26 de Oct. de 2017
I am trying to get a graph with two y variables, both having individual y axes. The y axes and the x-axis should all be logarithmic.
Creating the two y axes using yyaxis seems to work. However, when I try to modify both axes, only the secondary y-axis seems to be in a logarithmic scale. The primary y-axis is linear.
My code:
loglog(f,Gp,'x','MarkerSize',6,'LineWidth',3);
yyaxis left
ylabel('G_{prime} (Pa)')
yyaxis right
ylabel('G_{doubleprime} (Pa)');
hold on
loglog(f,Gdp,'x','MarkerSize',6,'LineWidth',3);
xlabel('Angular Frequency (rad/s)');
set(findall(0,'YAxisLocation','right'),'Yscale','log');
set(findall(0,'YAxisLocation','left'),'Yscale','log');
Attempted solutions:
- I have tried to change both the order and the position of the set(findall(...)) function, but to no avail.
- I have also attempted to solve the problem with plotyy(f,Gp,f,Gdp,@loglog,@loglog). This also did not provide me with the desired result.
- Interchanging the 'loglog' function that creates the plots with 'plot', 'semilogy', or 'semilogx' does not work either.
Some other details:
- Gp, Gdp and f are all 73x1 double.
- Both curves are visible.
- No errors are printed to the console.
Can anyone provide a solution?

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Oct. de 2017
My tests show that it is creating a log plot. However, in that range of y values, it is not visibly apparent that the line spacing is log rather than linear. If you compare the plot to semilogx(1:73, log(Gp)) you will see the shape is correct for the loglog plot.
  1 comentario
Pim Wijnands
Pim Wijnands el 26 de Oct. de 2017
Thanks! It did cross my mind, but I trusted my eyes too much I guess.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 26 de Oct. de 2017
This code worked fine for me in release R2017b on Windows and showed two Y axes and one X axis, all three in log scale. Try it in your MATLAB session. If it doesn't work for you, use print to create an image from the figure and use the Image button (the one with a green rectangle inside a brown frame) above the comment entry box to include it in your response.
x = 1:10;
y1 = 5*x;
y2 = x.^2;
yyaxis left
h = loglog(x, y1);
yyaxis right
h2 = loglog(x, y2);
  1 comentario
Pim Wijnands
Pim Wijnands el 26 de Oct. de 2017
Dear Steven,
Thanks for your reply. Your code worked on my MATLAB R2017a as well, so I broke down my code even further and assembled it piece by piece. It turns out that MATLAB is unable to plot the values of my 'Gp' variable on a logarithmic scale, whereas it does work for 'Gdp'. A simple
loglog(1:length(Gp),Gp)
Does not create the desired loglog plot. It does work on Gdp, so I attached a .mat file containing my variables f, Gp and Gdp. As far as I can see, there are no notable differences between Gp and Gdp. Both share their first three NaN entries and are 73x1 doubles.

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by