MATLAB semilogx in MATLAB®

Learn how to make 9 semilogx charts in MATLAB, then publish them to the Web with Plotly.


Plot One Line

Define x as a vector of logarithmically spaced values from 0.1 to 100, and define y as a copy of x. Create a linear-log plot of x and y, and call the grid function to show the grid lines.

x = logspace(-1,2);
y = x;
semilogx(x,y)
grid on

fig2plotly()

Plot Multiple Lines

Create a vector of logarithmically spaced x-coordinates and two vectors of y-coordinates. Plot two lines by passing comma-separated x-y pairs to semilogx.

x = logspace(-1,2);
y1 = x;
y2 = -x;
semilogx(x,y1,x,y2)
grid on

fig2plotly()

Specify Axis Labels and Tick Values

Define f as a vector containing the frequencies from 10 Hz to 100,000 Hz. Define gain as a vector of power gain values in decibels. Then plot the gain values against frequency.

f = logspace(1,5,100);
v = linspace(-50,50,100);
gain = (1-exp(5*(2.5*v.^2)./7500))/14;
semilogx(f,gain)
grid on

fig2plotly()
10251002510002510,00025100,000−5−4.5−4−3.5−3−2.5−2−1.5−1−0.50

Call the yticks function to reposition the y-axis tick values at whole-number increments along the y-axis. Then create x- and y-axis labels by calling the xlabel and ylabel functions.

yticks([-5 -4 -3 -2 -1 0])
xlabel ('Freqency (Hz)')
ylabel('Power Gain (dB)')

fig2plotly()
10251002510002510,00025100,000−5−4−3−2−10
Freqency(Hz)PowerGain(dB)

Plot Points as Markers Without Lines

Create a set of x- and y-coordinates and display them in a linear-log plot. Specify the line style as 'o' to display circular markers without connecting lines. Specify the marker fill color as the RGB triplet [0 0.447 0.741], which corresponds to a dark shade of blue.

x = logspace(-1,2,15);
y = 12 + x;
semilogx(x,y,'o','MarkerFaceColor',[0 0.447 0.741])
grid on

fig2plotly()

Add a Legend

Create a vector of logarithmically spaced x-coordinates and two vectors of y-coordinates. Then plot two lines by passing comma-separated x-y pairs to semilogx. Display a legend by calling the legend function.

x = logspace(1,4,100);
v = linspace(-50,50,100);
y1 = 100*exp(-1*((v+5).^2)./200);
y2 = 100*exp(-1*(v.^2)./200);
semilogx(x,y1,x,y2,'--')
legend('Measured','Estimated')
grid on

fig2plotly()
10251002510002510,0000102030405060708090100
MeasuredEstimated

Specify y-Coordinates Only

When you specify only one coordinate vector, semilogx plots those coordinates against the values 1:length(y). For example, define y as a vector of 5 values between 0 and 40. Create a linear-log plot of y.

y = [0 10 20 30 40];
semilogx(y)
grid on

fig2plotly()

If you specify y as a matrix, the columns of y are plotted against the values 1:size(y,1). For example, define y as a 5-by-3 matrix and pass it to the semilogx function. The resulting plot contains 3 lines, each of which has x-coordinates that range from 1 to 5.

y = [ 0    10    20
     10    20    30
     20    30    40
     30    40    50
     40    50    60];

semilogx(y)
grid on

fig2plotly()

Specify Target Axes

Create a tiled chart layout in the 'flow' tile arrangement, so that the axes fill the available space in the layout. Next, call the nexttile function to create an axes object and return it as ax1. Then display a linear-log plot by passing ax1 to the semilogx function.

tiledlayout('flow')
ax1 = nexttile;
x = logspace(-1,2);
y1 = 1./x;
semilogx(ax1,x,y1)

fig2plotly()

Repeat the process to create a second linear-log plot.

ax2 = nexttile;
y2 = x;
semilogx(ax2,x,y2)

fig2plotly()

Change Line Appearance After Plotting

Create a linear-log plot containing two lines, and return the line objects in the variable slg.

x = logspace(-1,2); y1 = x; y2 = -x; slg = semilogx(x,y1,x,y2);

Change the width of the first line to 3, and change the color of the second line to purple.

slg(1).LineWidth = 3;
slg(2).Color = [0.4 0 1];

fig2plotly()

Plot Discontinuous Function

Insert NaN values wherever there are discontinuities in your data. The semilogx function displays gaps at those locations.

Create a pair of x- and y-coordinate vectors. Replace the fortieth y-coordinate with a NaN value. Then create a linear-log plot of x and y.

x = logspace(-1,2);
y = x;
y(40) = NaN;
semilogx(x,y)

fig2plotly()