✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.

Line Charts in MATLAB®

How to make a plot in MATLAB®. Examples of the plot function, line and marker types, custom colors, and log and semi-log axes.


x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);

fig = figure;
plot(x,y1,x,y2);

%--PLOTLY--%

% Strip MATLAB<sup>&reg;</sup> style by default!
response = fig2plotly(fig);
[v,T,vT] = xlsread('data-matlab-excel-example.xlsx','Sheet1','A1:B13') 
% 'xlsx' for excel 2007

x=v(:,1);
y=v(:,2);

plot(x,y);
title('Plotting from Excel Data');

response = fig2plotly();
The data file for this chart can be downloaded from Here.
x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

fig = figure;
hold on

plot(x,y1,'Color',[50 204 10]/255,'LineWidth',3,'LineStyle','-.');
plot(x,y2,'Color',[21 24 100]/255,'LineWidth',3,'LineStyle','--');
plot(x,y3,'Color',[201 24 50]/255,'LineWidth',2,'LineStyle','o');

%--PLOTLY--%

% strip = false =&gt; preserve MATLAB<sup>&reg;</sup> style!

response = fig2plotly(fig, 'strip', false);
x=[1 2 3 4 5 6 7 8 9 10];
y=[1.2012e-03   2.3046e-03   2.8067e-03   3.0078e-03   5.7281e-03   5.2235e-03   6.7391e-03   7.1429e-03   6.3348e-03   1.0698e-02];
xi=1:0.01:10;

yi=interp1(x, y, xi, 'spline');
plot(xi, yi);

response = fig2plotly();
% Generate some data using the besselj function
x = 0:0.2:10;
y0 = besselj(0,x);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
y6 = besselj(6,x);

% Plot the points from the Bessel functions using standard marker types
fig = figure;
plot(x, y0, 'r+', x, y1, 'go', x, y2, 'b*', x, y3, 'cx', ...
    x, y4, 'ms', x, y5, 'yd', x, y6, 'kv');

%--PLOTLY--%

% strip = false =&gt; preserve MATLAB<sup>&reg;</sup> style!

response = fig2plotly(fig, 'strip', false);
X = linspace(0,4*pi,40);
Y = cos(X).*exp(-X);

fig = figure;
stairs(Y);
response = fig2plotly(fig,'strip',false);
fig=figure;
hax=axes;
x=0:0.1:10;
hold on
plot(x,3+5*exp(-x));

% Vertical Line
line([4 4],get(hax,'XLim'))
% Horizontal Line
line(get(hax,'YLim'),[3 3]);

response = fig2plotly(fig,'strip',false);
% Create some data
eb = 0:5;
SER = [0.1447 0.1112 0.0722 0.0438 0.0243 0.0122];
BER = [0.0753 0.0574 0.0370 0.0222 0.0122 0.0061];

% Create a y-axis semilog plot using the semilogy function
% Plot SER data in blue and BER data in red
fig = figure;
semilogy(eb, SER, 'bo-');
hold on;
semilogy(eb, BER, 'r^-');

% Turn on the grid
grid on;

% Add title and axis labels
title('Performance of Baseband QPSK');
xlabel('EbNo (dB)');
ylabel('SER and BER');

%--PLOTLY--%

% Strip MATLAB<sup>&reg;</sup> style by default!
response = fig2plotly();
% Create the equation..
%%
b0 = 3;
b1 = 4;
f = @(x) b0-b1*x;
%%

% Plot the equation..
ezplot( f, 0, 5 );
title('Plot Symbolic Linear Equation')
response = fig2plotly();
Inspired from Matlab Forum
% Defining the function over interval..
f1 = @(x) log(x);
f2 = @(x) 2.3026 * exp(10-x);

fplot(f1, [1 10]);
hold on
fplot(f2, [10 15]);
hold off;

title('Plotting Function over interval');
response = fig2plotly();
zeta = [0.01 .02 0.05 0.1 .2 .5 1 ];
colors = ['r' 'g' 'b' 'c' 'm' 'y' 'k'];

w = logspace(-1, 1, 1000);

figure;
for i = 1:7
    a = w.^2 - 1;
    b = 2*w*zeta(i);
    gain = sqrt(1./(a.^2 + b.^2));
    loglog(w, gain, 'color', colors(i), 'linewidth', 2);
    hold on;
end

% Set the axis limits
axis([0.1 10 0.01 100]);

% title and labels
title('|G|[omega]  vs omega');
xlabel('omega');
ylabel('|G|[omega]');

%--PLOTLY--%

% Strip MATLAB<sup>&reg;</sup> style by default!
response = fig2plotly();
% sampling rate
fs = 500;

% duration
dur = 1;

% time vector
t = 1 + linspace(-dur,dur,fs);

% signal
sig = [t(1:length(t)/2) t(1:length(t)/2)];

% noise
sign = sig + 0.1*randn(1,length(sig));

% plot signal + noise
fig = figure;
sp = plot(t,sig,'LineWidth',8);
hold on
sn = plot(t,sign,'ro');

% title/labels
title('Singal Noise');
xlabel('Time (s.)');
ylabel('Amplitude');

%--PLOTLY--%

% Strip MATLAB<sup>&reg;</sup> style by default!
response = fig2plotly(fig);
trace1 = struct(...
  'x', [1, 2, 3, 4], ...
  'y', [10, 15, 13, 17], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [1, 2, 3, 4], ...
  'y', [16, 5, 11, 9], ...
  'type', 'scatter');
data = {trace1, trace2};
response = plotly(data, struct('filename', 'basic-line2', 'fileopt', 'overwrite'));
plot_url = response.url