How to set YLim for multiple Yaxis on UIaxes
3 次查看(过去 30 天)
显示 更早的评论
I am plotting a figure using uiaxes in app designer with 2 y axis one on the left and other on the right. And I am trying to set YLim for both left and right. But the code I wrote was wrong. The one I right for the right is overwriting the one on the left. So technically I am only setting YLim for the left one.
yyaxis(app.UIAxes_4,'left');
plot(app.UIAxes_4,app.actualTime,app.Charge_Current);
app.UIAxes_4.XMinorGrid ="on";
app.UIAxes_4.YMinorGrid ="on";
app.UIAxes_4.YLabel.String = 'Current [A]';
app.UIAxes_4.YLabel.FontSize = 12;
app.UIAxes_4.YLabel.FontWeight ="bold";
app.UIAxes_4.YLim = [app.Y1min app.Y1max]; % This sets the YLim correctly for the left side
yyaxis(app.UIAxes,'right');
plot(app.UIAxes,app.actualTime,voltage);
app.UIAxes.XMinorGrid ="on";
app.UIAxes.YMinorGrid ="on";
app.UIAxes.YLabel.String = 'Current [A]';
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight ="bold";
app.UIAxes_4.YLim = [app.Y2min app.Y2max]; % This overrides the Ylim set earlier and sets a new YLim for
% left side without setting
% Ylim for the right side.
What is the way to set Ylim on both left and right. Thank you.
4 个评论
Shivam
2024-9-11
Could you attach your app for me to debug the issue? Use the paperclip icon to attach it.
回答(2 个)
Jatin
2024-9-11
编辑:Jatin
2024-9-12
The line 'yyaxis(app.UIAxes, 'right')' activates the right y-axis for the specified parent, which is app.UIAxes.
Since you haven't used 'yyaxis(app.UIAxes_4, 'right')', your attempt to set the YLim for the right y-axis of app.UIAxes_4 is actually applied to the left y-axis in the last line, as it remains the active side for the app.UIAxes_4 axes.
Refer to the documentation below to learn more about the 'yyaxis':
Hope it helps!
2 个评论
nick
2024-9-11
Hi Govind,
I understand that you want to create a chart with two y-axes each having separate limits.
When the right side of the UI axes is activated using the function 'yyaxis' , it acts upon the object 'app.UIAxes' instead of the 'app.UIAxes_4.YLim' object. This causes the right side of the 'app.UIAxes_4.YLim' object to remain inactive and causes the right to overwrite the one on the left. Here's a sample MATLAB script that implements the two y-axes with different limit:
time = linspace(0, 10, 100); % Time from 0 to 10 seconds
current = sin(time); % Dummy current data
voltage = 10*cos(time); % Dummy voltage data
% Plot on the left y-axis
yyaxis(app.UIAxes, 'left');
plot(app.UIAxes, time, current, '-b', 'DisplayName', 'Current');
app.UIAxes.YLabel.String = 'Current [A]';
app.UIAxes.YLim = [-1.5, 1.5]; % Example Y limits for the left axis
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight = 'bold';
% Plot on the right y-axis
yyaxis(app.UIAxes, 'right');
plot(app.UIAxes, time, voltage, '-r', 'DisplayName', 'Voltage');
app.UIAxes.YLabel.String = 'Voltage [V]';
app.UIAxes.YLim = [-15, 15]; % Example Y limits for the right axis
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight = 'bold';
% Add grid and title
app.UIAxes.XMinorGrid = 'on';
app.UIAxes.YMinorGrid = 'on';
title(app.UIAxes, 'Current and Voltage vs. Time');
xlabel(app.UIAxes, 'Time [s]');
legend(app.UIAxes, 'show');
You can refer to the following documentation to learn more about the function 'yyaxis':
Hope this helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!