Can the same axis scaling be obtained for the left and right y axes using "yyaxis" in MATLAB R2024a?

5 vues (au cours des 30 derniers jours)
When I execute the command "yyaxis right", the right y-axis is a different color and scale than the left y-axis.
How can I use the “yyaxis” function to create a right y-axis that is identical in color and scale to the left y-axis?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 15 Oct 2025
While there is no “yyaxis” input argument or single MATLAB function that will create identical left and right axes, a workaround is to create a right axis using properties of the existing left axis. For example, instead of executing "yyaxis right", the following two lines of code can be used:
hL = gca; hR = axes('YLim', hL.YLim, 'YTick', hL.YTick, 'YTickLabels', hL.YTickLabels, ... 'XTick', [], 'YAxisLocation', 'right', 'Color', 'none');
Here, “hL” is a handle to the existing left axis, and “hR” is a handle to the newly created right axis. Since “hR” is created with the same y-axis limits, tick values, and tick labels as “hL”, the resulting right y-axis should appear identical to the left y-axis. 

Plus de réponses (0)

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by