Skip to content

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.

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.

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.5
x = logspace(-1, 2, 100); % 100 points from 10^-1 to 10^2
y = 5 * x.^1.5;
% Create the log-log plot
figure; % Create a new figure window
loglog(x, y, 'm-s', 'LineWidth', 1.5, 'MarkerFaceColor', 'y');
% Add labels and a title for clarity
title('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 readability

Expected 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 use a logarithmic scale on one axis and a linear scale on the other.

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 plot
figure;
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.

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 filter
f = logspace(1, 6, 200); % Frequencies from 10 Hz to 1 MHz
R = 1000; % 1k Ohm
C = 1e-6; % 1 uF
% Calculate magnitude
mag = 1 ./ sqrt(1 + (2*pi*f*R*C).^2);
% Create the semi-log plot
figure;
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.

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 line
p1 = loglog(x, y1);
hold on; % Hold the plot to add another line
p2 = 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.