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

# Line and Scatter in MATLAB®

How to make a Line and Scatter graphs in MATLAB®.

### Plot a Line of Best Fit

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

D = [0 4 8 12 16 20 24 28 32];
F = [0 .23 .36 .43 .52 .64 .78 .85 .92];
plot(D,F,'.b')
p = polyfit(D,F,1)
f = polyval(p,D);
hold on
plot(D,f,'--r')

resp = fig2plotly;
plotly_url = resp.url;


Inspired from Matlab Forum

### Matlab Compute and plot Quadratic Fit

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

x = linspace(0,4*pi,10);
A = 2, B = 3, C = 1;

y = A * x.^2 + B * x + C

p = polyfit(x,y,2);

k = polyval(p,x)

fig = figure;
hold on
plot(x,y,'ro')
plot(x,k,'b-')
hold off

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url;

### Quantile Plot (qqplot) with two distributions

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

fig = figure;
qqplot(price1,price2);
title('Quantile Quantile Plot with two distributions')

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url

### Plot Points and Line

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

x = 1:1:100;
y = randi(100,1,100)
fig = figure;
hold on
scatter(x,y);
plot(x+2,y)
hold off

resp = fig2plotly(fig)
plotly_url = resp.url



### Plot Multiple Lines and Curves

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

x = linspace(1,10,10)
y = randi(10,1,10)

x1 = linspace(1,2*pi, 100)

fig = figure;
hold on
plot(x,y)
plot([1 10],[5 5])
plot([-5 5],[1 10])
plot(x1, sin(x1))
plot(x1, exp(x1))
xlim([-6 10])
ylim([-10 10])
hold off;

resp = fig2plotly(fig, 'strip',false)
plotly_url = resp.url;


MATLAB is a registered trademark of The MathWorks, Inc. 