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

조회 수: 16 (최근 30일)
Pim Wijnands
Pim Wijnands 2017년 10월 26일
댓글: Pim Wijnands 2017년 10월 26일
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?

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 26일
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
Pim Wijnands
Pim Wijnands 2017년 10월 26일
Thanks! It did cross my mind, but I trusted my eyes too much I guess.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 10월 26일
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
Pim Wijnands
Pim Wijnands 2017년 10월 26일
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.

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by