Show Sidebar Hide Sidebar

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.

% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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

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

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-basic-line');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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 => preserve MATLAB style!

response = fig2plotly(fig, 'filename', 'matlab-line-style', 'strip', false);
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

[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;
plotly_url = response.url;
The data file for this chart can be downloaded from Here.
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% 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 => preserve MATLAB style!

response = fig2plotly(fig, 'filename', 'matlab-line-markers', 'strip', false);
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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;
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% 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 style by default!
response = fig2plotly(fig, 'filename', 'matlab-semi-logy');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

X = linspace(0,4*pi,40);
Y = cos(X).*exp(-X);

fig = figure;
stairs(Y);
response = fig2plotly(fig,'strip',false);
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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);
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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 style by default!
response = fig2plotly(gcf, 'filename', 'matlab-log-log-line');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% 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;
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% 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;
plotly_url = response.url;
Inspired from Matlab Forum
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% 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 style by default!
response = fig2plotly(fig, 'filename', 'matlab-line-and-scatter-basic');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

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
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.