Show Sidebar Hide Sidebar

Contour Plots in MATLAB

How to make a contour plot in MATLAB. Seven examples of contour plots of matrices with subplots, custom color-scales, and smoothing.

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

[X,Y,Z] = peaks;
contour(X,Y,Z,20)

% PLOTLY
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

x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);

figure
contour(X,Y,Z)

% PLOTLY
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

surf(peaks);
response = fig2plotly;

% Convert data to a struct, and apply custom colorscale
data = cell2struct(response.data,'data',1);
data.colorscale = 'Viridis';
response2 = plotly(data, struct('layout',response.layout));
plotly_url = response2.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

size = 100;
x = linspace(-2*pi, 2*pi, size);
y = linspace(-2*pi, 2*pi, size);
z = zeros(size, size);
for n = 1:size
    for m = 1:size
        r2 = x(n)^2 + y(m)^2;
        z(n, m) = sin(x(n))*cos(y(m))*sin(r2)/log(r2+1);
    end
end

data = {...
  struct(...
    'z', z, ...
    'x', x, ...
    'y', y, ...
    'type', 'contour')...
};
response = plotly(data, struct('filename', 'simple-contour', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

t = linspace(-1, 1.2, 2000);
x = (t.^3)+(0.3.*randn(1, 2000));
y = (t.^6)+(0.3.*randn(1, 2000));

trace1 = struct(...
  'x', x, ...
  'y', y, ...
  'mode', 'markers', ...
  'name', 'points', ...
  'marker', struct(...
    'color', 'rgb(102,0,0)', ...
    'size', 2, ...
    'opacity', 0.4), ...
  'type', 'scatter');
trace2 = struct(...
  'x', x, ...
  'y', y, ...
  'name', 'density', ...
  'ncontours', 20, ...
  'colorscale', 'Hot', ...
  'reversescale', true, ...
  'showscale', false, ...
  'type', 'histogram2dcontour');
trace3 = struct(...
  'x', x, ...
  'name', 'x density', ...
  'marker', struct('color', 'rgb(102,0,0)'), ...
  'yaxis', 'y2', ...
  'type', 'histogram');
trace4 = struct(...
  'y', y, ...
  'name', 'y density', ...
  'marker', struct('color', 'rgb(102,0,0)'), ...
  'xaxis', 'x2', ...
  'type', 'histogram');
data = {trace1, trace2, trace3, trace4};
layout = struct(...
    'showlegend', false, ...
    'autosize', false, ...
    'width', 600, ...
    'height', 550, ...
    'xaxis', struct(...
      'domain', [0, 0.85], ...
      'showgrid', false, ...
      'zeroline', false), ...
    'yaxis', struct(...
      'domain', [0, 0.85], ...
      'showgrid', false, ...
      'zeroline', false), ...
    'margin', struct('t', 50), ...
    'hovermode', 'closest', ...
    'bargap', 0, ...
    'xaxis2', struct(...
      'domain', [0.85, 1], ...
      'showgrid', false, ...
      'zeroline', false), ...
    'yaxis2', struct(...
      'domain', [0.85, 1], ...
      'showgrid', false, ...
      'zeroline', false));
response = plotly(data, struct('layout', layout, 'filename', '2dhistogram-contour-subplots', 'fileopt', 'overwrite'));
plot_url = response.url
Add slider controls to contour plots with the postMessage API.

See the code on JSFiddle.

Watch the 5 second video of how it works.
Still need help?
Contact Us

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