BodePlot: Plotting straight lines on the Mag and Phase plots. Can access the phase plot, to do so, but not the mag plot

Hi there
I was customising the standard MATLAB Bode plot to make the gridlines, major and minor bold,
change the x and y label size and bold them and also customise the axis.
Then I tried to plot the straight line approximation, but I can only seem to access the phase plot, and not the magnitude plot.
For the two axes, ax(1) and ax(2), I have called them mag_ax and phase_ax.
I can access the other properties fine, but when I tries to add the lines, my code doesn't recognise the mag_ax.
Any advice greatly appreciated
close all
s = tf('s');
G_1 = tf(1/(0.1*s + 1));
figure('WindowState','maximized');
gg = bodeplot(G_1);
title('Bode plot of $\frac{1}{0.1s+1}$',...
'Interpreter','latex','FontSize',18,'FontWeight','bold');
gg.Responses.Color = "blue";
gg.Responses.LineWidth = 2;
gg.XLabel.FontWeight = "bold";
% Get the handles of the axes
ax = findobj(gcf,'type','axes');
mag_ax = ax(1);
phase_ax = ax(2);
mag_ax.XLim = [0.1 1000];
mag_ax.YLim= [-60 +60];
phase_ax.XLim = [0.1 1000];
phase_ax.YLim= [-90 +90];
phase_ax.YTick = (-90:45:90);
grid on
grid minor
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.MinorGridLineStyle = '-';
mag_ax.MinorGridLineStyle = '-';
phase_ax.MinorGridLineWidth = 0.75;
mag_ax.MinorGridLineWidth = 0.75;
phase_ax.MinorGridAlpha = 0.5;
mag_ax.MinorGridAlpha = 0.5;
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.GridAlpha = 1;
mag_ax.GridAlpha = 1;
mag_ax.YLabel.FontWeight = "bold";
mag_ax.YLabel.FontSize = 12;
phase_ax.YLabel.FontWeight = "bold";
phase_ax.YLabel.FontSize = 12;
for k = 1:length(phase_ax)
xticks(phase_ax(k), [0.1 1 10 100 1000]); % Set desired tick positions
end
for k = 1:length(phase_ax)
phase_ax(k).XTickLabel = arrayfun(@(x) sprintf('%.1f', x), phase_ax(k).XTick, 'UniformOutput', false);
end
set(phase_ax,'fontweight','bold')
set(mag_ax,'fontweight','bold')
gg.Responses.Visible = 'on';
gg.Responses.Color = 'blue';
mag_ax.NextPlot = 'add';
line([0.1 10.0],[0 0],'linewidth',2,'color','red');
line([10.0 1000],[0 -40],'linewidth',2,'color','red');
phase_ax.NextPlot = 'add';
line([0.1 1.0],[0 0],'linewidth',2,'color','red');
line([1.0 100],[0 -90],'linewidth',2,'color','red');
line([100 1000],[-90 -90],'linewidth',2,'color','red');

 Accepted Answer

Looks like the mag and phase axes were reversed, specify the axis input to the line commands. The NextPlot commands aren't needed because line doesn't delete other graphics objects or reset properties.
s = tf('s');
G_1 = tf(1/(0.1*s + 1));
figure('WindowState','maximized');
gg = bodeplot(G_1);
title('Bode plot of $\frac{1}{0.1s+1}$',...
'Interpreter','latex','FontSize',18,'FontWeight','bold');
gg.Responses.Color = "blue";
gg.Responses.LineWidth = 2;
gg.XLabel.FontWeight = "bold";
% Get the handles of the axes
ax = findobj(gcf,'type','axes');
% corrected here
%mag_ax = ax(1);
%phase_ax = ax(2);
mag_ax = ax(2);
phase_ax = ax(1);
mag_ax.XLim = [0.1 1000];
mag_ax.YLim= [-60 +60];
phase_ax.XLim = [0.1 1000];
phase_ax.YLim= [-90 +90];
phase_ax.YTick = (-90:45:90);
grid on
grid minor
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.MinorGridLineStyle = '-';
mag_ax.MinorGridLineStyle = '-';
phase_ax.MinorGridLineWidth = 0.75;
mag_ax.MinorGridLineWidth = 0.75;
phase_ax.MinorGridAlpha = 0.5;
mag_ax.MinorGridAlpha = 0.5;
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.GridAlpha = 1;
mag_ax.GridAlpha = 1;
mag_ax.YLabel.FontWeight = "bold";
mag_ax.YLabel.FontSize = 12;
phase_ax.YLabel.FontWeight = "bold";
phase_ax.YLabel.FontSize = 12;
for k = 1:length(phase_ax)
xticks(phase_ax(k), [0.1 1 10 100 1000]); % Set desired tick positions
end
for k = 1:length(phase_ax)
phase_ax(k).XTickLabel = arrayfun(@(x) sprintf('%.1f', x), phase_ax(k).XTick, 'UniformOutput', false);
end
set(phase_ax,'fontweight','bold')
set(mag_ax,'fontweight','bold')
gg.Responses.Visible = 'on';
gg.Responses.Color = 'blue';
% specify axis handle for line plots
mag_ax.NextPlot = 'add';
line(mag_ax,[0.1 10.0],[0 0],'linewidth',2,'color','red');
line(mag_ax,[10.0 1000],[0 -40],'linewidth',2,'color','red');
phase_ax.NextPlot = 'add';
line(phase_ax,[0.1 1.0],[0 0],'linewidth',2,'color','red');
line(phase_ax,[1.0 100],[0 -90],'linewidth',2,'color','red');
line(phase_ax,[100 1000],[-90 -90],'linewidth',2,'color','red');

1 Comment

Hi Paul,
Thank you for the reply and the details on the issue.
It seems like there is an issue with the axis depending on where the code is run
When I ran your code the the deshtop MATLAB, there doesn't seem to be the need to change the order of the axis, the original runs as expected, both as a live script and a script.
But, when it is run here in the online version and directly on MATLAB online, the axis seems to be swapped, hence your observation that the axis needed to be changed.
I did a little loop to check the ylabels, and swap the axis if necessary
Thanks for the information on the line command, new to me, really appreciate your expertise and time on answer my question.
Many thanks
Gerard
s = tf('s');
G_1 = tf(1/(0.1*s + 1));
figure('WindowState','maximized');
gg = bodeplot(G_1);
title('Bode plot of $\frac{1}{0.1s+1}$',...
'Interpreter','latex','FontSize',18,'FontWeight','bold');
gg.Responses.Color = "blue";
gg.Responses.LineWidth = 2;
gg.XLabel.FontWeight = "bold";
% Get the handles of the axes
ax = findobj(gcf,'type','axes');
% Ensure correct assignment by checking y-labels
% so it doesn't matter if online or desktop
% or more probally different versions of MATLAB
ylabs = arrayfun(@(a) a.YLabel.String, ax, 'UniformOutput', false);
if contains(ylabs{1}, 'Magnitude')
mag_ax = ax(1); phase_ax = ax(2);
else
mag_ax = ax(2); phase_ax = ax(1);
end
mag_ax.XLim = [0.1 1000];
mag_ax.YLim= [-60 +60];
phase_ax.XLim = [0.1 1000];
phase_ax.YLim= [-90 +90];
phase_ax.YTick = (-90:45:90);
grid on
grid minor
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.MinorGridLineStyle = '-';
mag_ax.MinorGridLineStyle = '-';
phase_ax.MinorGridLineWidth = 0.75;
mag_ax.MinorGridLineWidth = 0.75;
phase_ax.MinorGridAlpha = 0.5;
mag_ax.MinorGridAlpha = 0.5;
phase_ax.GridLineWidth = 1;
mag_ax.GridLineWidth = 1;
phase_ax.GridAlpha = 1;
mag_ax.GridAlpha = 1;
mag_ax.YLabel.FontWeight = "bold";
mag_ax.YLabel.FontSize = 12;
phase_ax.YLabel.FontWeight = "bold";
phase_ax.YLabel.FontSize = 12;
for k = 1:length(phase_ax)
xticks(phase_ax(k), [0.1 1 10 100 1000]); % Set desired tick positions
end
for k = 1:length(phase_ax)
phase_ax(k).XTickLabel = arrayfun(@(x) sprintf('%.1f', x), phase_ax(k).XTick, 'UniformOutput', false);
end
set(phase_ax,'fontweight','bold')
set(mag_ax,'fontweight','bold')
gg.Responses.Visible = 'on';
gg.Responses.Color = 'blue';
line(mag_ax,[0.1 10.0],[0 0],'linewidth',2,'color','red');
line(mag_ax,[10.0 1000],[0 -40],'linewidth',2,'color','red');
line(phase_ax,[0.1 1.0],[0 0],'linewidth',2,'color','red');
line(phase_ax,[1.0 100],[0 -90],'linewidth',2,'color','red');
line(phase_ax,[100 1000],[-90 -90],'linewidth',2,'color','red');

Sign in to comment.

More Answers (1)

I would recommend avoiding the use of "findobj" in this manner. Utilizing the chart API is the preferred (and more supported!) approach to this kind of problem.
Here is my take on generating your desired plot using only the chart API. The convience commands you used like "grid" and "title" are supported, but I'm more of a fan of directly interacting with the chart handle.
G_1 = tf(1,[0.1 1]);
gg = bodeplot(G_1);
% Style labels
gg.Title.String = "Bode plot of $\frac{1}{0.1s+1}$";
gg.Title.Interpreter = "latex";
gg.Title.FontSize = 18;
gg.Title.FontWeight = "bold";
gg.XLabel.FontWeight = "bold";
gg.YLabel.FontWeight = "bold";
gg.YLabel.FontSize = 12;
gg.XLimits = [0.1 1000];
gg.YLimits = {[-60 60];[-90 90]};
% Style axes
gg.AxesStyle.GridVisible = "on";
gg.AxesStyle.GridLineWidth = 1;
gg.AxesStyle.GridAlpha = 1;
gg.AxesStyle.MinorGridVisible = "on";
gg.AxesStyle.MinorGridLineWidth = 0.75;
gg.AxesStyle.MinorGridLineStyle = "-";
gg.AxesStyle.MinorGridAlpha = 0.5;
gg.AxesStyle.FontWeight = "bold";
% Style response
gg.Responses(1).LineWidth = 2;
% Add extra data
gg.NextPlot = "add";
gg.DataAxes = [1 1]; %target mag axes
line([0.1 10.0],[0 0],Linewidth=2,SeriesIndex=2);
line([10.0 1000],[0 -40],Linewidth=2,SeriesIndex=2);
gg.DataAxes = [2 1]; %target phase axes
line([0.1 1.0],[0 0],Linewidth=2,SeriesIndex=2);
line([1.0 100],[0 -90],Linewidth=2,SeriesIndex=2);
line([100 1000],[-90 -90],Linewidth=2,SeriesIndex=2);

3 Comments

Just want to point out that the DataAxes property only arrived in 2025a.
Thanks Andrew and Paul for both your reponses.
The chart API looks like the cleanest option to go with.
Thanks to you both on the DataAxes property, new to me, but nice and clean targeting of the individual plots.
Really appreciate both of your inputs.
As Paul pointed out, DataAxes is new as of R2025a. The entire chart API is new as of R2024b!
With the newly released R2026a, you can perform the response styling in the "bodeplot" function call:
gg = bodeplot(G_1,SeriesIndex=1,LineWidth=2)

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 13 Apr 2026 at 12:09

Commented:

on 16 Apr 2026 at 16:15

Community Treasure Hunt

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

Start Hunting!