Plotting semilogy on two axes covering the same range, one side is log the other is not.

29 views (last 30 days)
I'm trying to make a figure with two vertical log axes, and the horizontal x axis still be linear. I need to compare plots who cover different ranges, so I made a new range that considers the floor(min()) of all values and the ceil(max()) of all values since in this case they're all negative. However, when I try to create both semi logy plots, the right axis still looks linear even though it covers the same range as the left axis which looks logarithmic. In other questions people have suggested that variables cannot be represented well on a logarithmic plot, but I don't think that is the case here since some of the values look fine in logarithmic.
Here is my code.
x = [3,5,7,9]';
A = [-5.3448 -9.2574 -5.3448 -9.2574;
-3.3636 -9.7099 -3.5704 -10.3067;
-2.6247 -10.6076 -2.6176 -10.5788;
-2.0660 -10.7350 -2.1397 -11.1182];
Y_range = [floor(min(A,[],'all')),ceil(max(A,[],'all'))];
figure
yyaxis left
semilogy(x,A(:,1),...
'LineStyle','-')
hold on
semilogy(x,A(:,3),...
'LineStyle','--')
ylim(Y_range)
yyaxis right
semilogy(x,A(:,2),...
'LineStyle','-')
hold on
semilogy(x,A(:,4),...
'LineStyle','--')
ylim(Y_range)
I confirmed my suspicions by creating a new figure before the yyaxis right.
I've attached PDFs showing the difference.
Together
Separate
Left
Right
As you can see, both data sets have no trouble plotting on log from the two separate figures, but only when they are on the same figures.

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 17 Jul 2019
Hi Nathaniel,
I think the problem is due to the “hold on” statement. The following code is producing the expected plot:
x = [3,5,7,9]';
A = [-5.3448 -9.2574 -5.3448 -9.2574;
-3.3636 -9.7099 -3.5704 -10.3067;
-2.6247 -10.6076 -2.6176 -10.5788;
-2.0660 -10.7350 -2.1397 -11.1182];
Y_range = [floor(min(A,[],'all')),ceil(max(A,[],'all'))];
figure
yyaxis left
semilogy(x,A(:,1),...
'LineStyle','-')
ylim(Y_range)
yyaxis right
semilogy(x,A(:,2),...
'LineStyle','-')
ylim(Y_range)
hold on
yyaxis left
semilogy(x,A(:,3),...
'LineStyle','--')
yyaxis right
semilogy(x,A(:,4),...
'LineStyle','--')

More Answers (0)

Categories

Find more on Graphics in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!