Hi @Chris Nemecek ,
Let me address your query regarding, “Is there a way to mimc 'axis equal' for a secondary axis? 'Axis equal' doesn't work when using yyaxis(ax,'right').In addtion, the mimic should resize the same as if 'axis equal' was called on a single plot.”
Please see my response to your comments below. So, first a sample data is generated for two different functions. Then, a figure is created, and the first y-axis is plotted. The second y-axis is activated, and the corresponding data is plotted along with the retrieval of limits of both axes , and the aspect ratio is calculated. Based on the aspect ratio, the limits of the axes are adjusted to mimic axis equal. Finally, the axis tight command is used to make sure that the axes fit the data closely. Here is completely example code,
% Sample Data
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x) * 10; % Scale to demonstrate secondary axis
% Create a figure
figure;
% Create the first axis
yyaxis left;
plot(x, y1, 'b-', 'LineWidth', 2);
ylabel('Sine Values');
xlabel('X Values');
grid on;
% Create the second axis
yyaxis right;
plot(x, y2, 'r-', 'LineWidth', 2);
ylabel('Cosine Values (scaled)');
% Mimic 'axis equal'
% Get limits for both axes
xLimits = xlim;
y1Limits = ylim; % Left axis limits
yyaxis right; % Activate right axis to get its limits
y2Limits = ylim; % Right axis limits
% Calculate aspect ratio
aspectRatio = (y1Limits(2) - y1Limits(1)) / (y2Limits(2) - y2Limits(1));
% Set equal aspect ratio
if aspectRatio > 1
% Adjust x limits based on aspect ratio
xlim([xLimits(1), xLimits(1) + (y1Limits(2) - y1Limits(1)) / aspectRatio]);
else
% Adjust y limits based on aspect ratio
ylim([y2Limits(1), y2Limits(1) + (xLimits(2) - xLimits(1)) * aspectRatio]);
end
% Final adjustments
axis tight; % Tighten the axes to fit the data
So, as you can see by looking at this code, it mimics the axis equal functionality for plots with dual y-axes. By calculating the aspect ratio and adjusting the limits accordingly, you will be satisfied that plots maintain a proportional relationship between the axes.
Please see attached.
Please let me know if you have any further questions.