Nils
Nils

Reputation: 930

Changing the plotting order with 2nd yaxis

I have an issue with changing the plotting order when I am plotting on two different y-axes. I want the plot on the left axis to be infront of the plot on the right yaxis. I already treid to change the order of calling the plotting functions, as you can see below. Further, I looked into this post:

Similar StackOverflow Question

But the accepted answer did not help me. I guess the issue is that I only change the order on one axis.. Also the command uistack only results in an error for I guess the same reason. Any help is highly appreciated.

My code:

V = horzcat((-2:0.1:2), (2:-0.1:-2));
t = (0:0.01234568:1.01);
T = (0:25:2025);

figure(30);   
yyaxis right
plot(t ,V, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Voltage / V');
set(gca,'FontSize',fontsize, 'LineWidth',2,'TickLength',[0.025 0.025])
xticks([0., 0.25, 0.5, 0.75, 1]);
ylim([-2, 2]),
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = (0:0.05:1);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (-2:0.25:2))
    
yyaxis left
top = plot(t ,T, 'Linewidth',3); hold all;
xlabel('Time / s');ylabel('Temperature / K');
ax = gca;
%ax.YAxis.MinorTick = 'on';
%ax.YAxis.MinorTickValues = (250:50:650);
set(ax.YAxis, 'MinorTick', 'on')
set(ax.YAxis, 'MinorTickValues', (250:50:650))
chH = get(gca,'Children');
set(gca,'Children',[chH(end);chH(1:end-1)]);
uistack(top, 'top');

What it looks like now:

enter image description here

As you can see the orange line is in front of the blue one, however I would like to have it the other way around.

Any help is highly appreciated.

Upvotes: 1

Views: 526

Answers (1)

saastn
saastn

Reputation: 6025

That's because the get(gca,'Children') returns only one line object! And it is the blue one. So this code actually does nothing. But I believe the problem comes from the yyaxis method. According to MATLAB docs:

Tips
...

  • The Children property of the Axes object only contains the children for the active side. To access all the children for both sides, use the allchild function.

Axes Properties
Axes properties related to the y-axis have two values. However, MATLAB® gives access only the value for the active side. For example, if the left side is active, then the YLim property of the Axes object contains the limits for the left y-axis. However, if the right side is active, then the YLim property contains the limits for the right y-axis.

What I get from above description is that when you call yyaxis, another axes object is created an placed above the original one. But they are somehow internally coupled, so you can access their properties, including their separated lists of children, by activating them after calling yyaxis again.

Upvotes: 1

Related Questions