Show Sidebar Hide Sidebar

# Scatter in MATLAB

How to make a scatter plot in MATLAB. Seven examples of the scatter function.

#### Simple Scatter Plot

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

% Load undersea elevation data
load seamount x y z;

% Create a scatter plot using the scatter function
fig = figure;
scatter(x, y, 10, z);

% Add title and axis labels
title('Undersea Elevation');
xlabel('Longitude');
ylabel('Latitude');

%--PLOTLY--%

% strip = false => preserve MATLAB style!

response = fig2plotly(fig, 'filename', 'matlab-scatter-plot', 'strip', false);
plotly_url = response.url;
``````

#### Scatter Plot from Array values ( Discrete Points )

``````% 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]
y = [3 4 6 3 6]
% Create a scatter plot using the scatter function

fig = figure;
scatter(x, y);

% Add title and axis labels
title('Simple Plot from array');
xlabel('X');
ylabel('Y');

%--PLOTLY--%

response = fig2plotly(fig, 'filename', 'matlab-scatter-plot-from-array');
plotly_url = response.url;
``````

#### Scatter Plot with Colors and Sizes

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

fig = figure;
s = 10;
c = linspace(1,10,length(x));
scatter(x,y,s,c)
zoom(2)

%--PLOTLY--%

% strip = false => preserve MATLAB style!

response = fig2plotly(fig, 'filename', 'matlab-seamount-scatter', 'strip', false);
plotly_url = response.url;
``````

#### Plotting Specific Array Column

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

a=[1:10;sin(1:10);cos(1:10)]';

fig = figure
scatter(a(:,1), a(:,2));
title('Sample Data');
response = fig2plotly(fig);
plotly_url = response.url;``````

#### Plotting Complex Data (Real and Imaginary Parts)

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

x = -2:0.25:2;
z1 = x.^exp(-x.^2);
z2 = 2*x.^exp(-x.^2);
real_z1 = real(z1);
imag_z1 = imag(z1);

real_z2 = real(z2);
imag_z2 = imag(z2);

plot(real_z1,imag_z1,'g*',real_z2,imag_z2,'bo');
title('Plotting Complex Data');
response = fig2plotly;
plotly_url = response.url;``````

#### Specifying Color List using `Hold On`

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

X = [linspace(0,100,100); linspace(0,100,100); linspace(0,100,100)]
Y = [randi(100,1,100); randi(100,1,100); randi(100,1,100)]

colors = ['r' 'g' 'b']
fig = figure;
hold on
for i=1:3
disp(colors(i));
scatter(X(i, :), Y(i, :),colors(i));
end;
hold off

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

#### Specifying Different Color for Different Points

``````% 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]
Y = [5 5 5 5 5]

fig = figure;
colors = ['r' 'g' 'b' 'y' 'c']

hold on;
for i=1:5
scatter(X(i), Y(i), 'LineWidth',10,'MarkerFaceColor',colors(i))
end
hold off;
resp = fig2plotly(fig)
plotly_url = resp.url;``````