MATLAB - Logarithmic Axes Plots
MATLAB - Creating Plots with Logarithmic Axes
Section titled “MATLAB - Creating Plots with Logarithmic Axes”Logarithmic plots are essential for visualizing data that spans several orders of magnitude or follows power-law or exponential trends. Unlike a linear scale with uniform spacing, a logarithmic scale compresses large value ranges, making trends more apparent. MATLAB provides dedicated functions for creating plots with logarithmic scales on one or both axes.
Why Use a Logarithmic Scale?
Section titled “Why Use a Logarithmic Scale?”You should consider a logarithmic scale when:
- Data covers a wide range: For example, plotting a signal’s power spectrum, where values can range from very small to very large.
- Highlighting exponential trends: Exponential growth or decay (
y = A*e^(kx)) appears as a straight line on a semi-log plot (linear x, log y). - Highlighting power-law trends: A power-law relationship (
y = A*x^k) appears as a straight line on a log-log plot.
Log-Log Plots: loglog()
Section titled “Log-Log Plots: loglog()”The loglog() function creates a plot where both the x-axis and y-axis use a base-10 logarithmic scale. This is ideal for visualizing power-law relationships.
Example: Plotting a Power-Law Relationship
Section titled “Example: Plotting a Power-Law Relationship”% Generate data following a power law: y = 5 * x^1.5x = logspace(-1, 2, 100); % 100 points from 10^-1 to 10^2y = 5 * x.^1.5;
% Create the log-log plotfigure; % Create a new figure windowloglog(x, y, 'm-s', 'LineWidth', 1.5, 'MarkerFaceColor', 'y');
% Add labels and a title for claritytitle('Power-Law Relationship on a Log-Log Plot');xlabel('X-axis (log scale)');ylabel('Y-axis (log scale)');grid on; % Add a grid for better readabilityExpected Output Description: This code generates a plot where both axes are logarithmic. Because the data follows a power-law, the resulting plot is a straight line. The slope of this line corresponds to the exponent (1.5). The line is magenta with square markers that have a yellow fill.
Semi-Log Plots: semilogx() and semilogy()
Section titled “Semi-Log Plots: semilogx() and semilogy()”Semi-log plots use a logarithmic scale on one axis and a linear scale on the other.
semilogy() - Logarithmic Y-axis
Section titled “semilogy() - Logarithmic Y-axis”The semilogy() function is perfect for visualizing exponential data, where the y-axis is logarithmic and the x-axis is linear.
% Generate data following an exponential growth: y = exp(0.5x)x = linspace(0, 10, 100);y = exp(0.5 * x);
% Create the semi-log plotfigure;semilogy(x, y, 'b--o', 'LineWidth', 1);
title('Exponential Growth on a Semi-Log Plot');xlabel('X-axis (linear scale)');ylabel('Y-axis (log scale)');grid on;Expected Output Description: This code generates a plot where the y-axis is logarithmic. The exponential data now appears as a straight line. The plot shows a blue dashed line with circle markers.
semilogx() - Logarithmic X-axis
Section titled “semilogx() - Logarithmic X-axis”The semilogx() function is used when the x-variable spans many orders of magnitude. A common application is in frequency response plots (Bode plots).
% Example: Frequency response of a simple RC filterf = logspace(1, 6, 200); % Frequencies from 10 Hz to 1 MHzR = 1000; % 1k OhmC = 1e-6; % 1 uF
% Calculate magnitudemag = 1 ./ sqrt(1 + (2*pi*f*R*C).^2);
% Create the semi-log plotfigure;semilogx(f, mag, 'r-', 'LineWidth', 2);
title('Frequency Response of an RC Low-Pass Filter');xlabel('Frequency (Hz) (log scale)');ylabel('Magnitude (linear scale)');grid on;Expected Output Description: This code generates a plot of a filter’s magnitude response. The x-axis (frequency) is logarithmic, which is standard for Bode plots. The plot shows a characteristic curve that is flat at low frequencies and then rolls off at higher frequencies.
Modern Plot Customization
Section titled “Modern Plot Customization”In modern MATLAB, it is best practice to get the plot object’s handle to modify its properties. This provides more control and leads to cleaner code.
x = logspace(-1, 2, 50);y1 = 10 * x.^2;y2 = 1000 ./ x;
figure;% Get handles for each plot linep1 = loglog(x, y1);hold on; % Hold the plot to add another linep2 = loglog(x, y2);hold off;
% --- Customize using handles ---p1.LineStyle = '--';p1.Color = 'blue';p1.LineWidth = 2;
p2.Marker = 'd';p2.MarkerEdgeColor = 'black';p2.Color = 'red';
grid on;title('Comparing Two Datasets');xlabel('Input');ylabel('Output');legend('y = 10x^2', 'y = 1000/x', 'Location', 'southwest');This approach is more readable and extensible than passing all properties as name-value pairs inside the plotting function.