Show Sidebar Hide Sidebar

Inset Plots in MATLAB

How to make an inset graph in MATLAB.

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

% Create data
t = linspace(0,2*pi);
t(1) = eps;
y = sin(t);

% Place axes at (0.1,0.1) with width and height of 0.8
fig = figure;
handaxes1 = axes('position', [0.1 0.1 0.8 0.8]);

% Main plot
plot(t, y);
xlabel('t'); ylabel('sin(t)');
set(handaxes1, 'box', 'off');

% Adjust XY label font
handxlabel1 = get(gca, 'xlabel');
set(handxlabel1, 'fontsize', 16, 'fontweight', 'bold');
handylabel1 = get(gca, 'ylabel');
set(handylabel1, 'fontsize', 16, 'fontweight', 'bold');

% Place second set of axes on same plot
handaxes2 = axes('position', [0.6 0.6 0.2 0.2]);
fill(t, y.^2, 'g');
set(handaxes2, 'box', 'off');
xlabel('t'); ylabel('(sin(t))^2');

% Adjust XY label font
set(get(handaxes2, 'xlabel'), 'fontname', 'times')
set(get(handaxes2, 'ylabel'), 'fontname', 'times')

% Add another set of axes
handaxes3 = axes('position', [0.25 0.25 0.2 0.2]);
plot(t, y.^3);
set(handaxes3, 'box','off');
xlabel('t'); ylabel('(sin(t))^3');

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-simple-inset');
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], ...
  'y', [4, 3, 2], ...
  'type', 'scatter');
trace2 = struct(...
  'x', [20, 30, 40], ...
  'y', [30, 40, 50], ...
  'xaxis', 'x2', ...
  'yaxis', 'y2', ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'yaxis2', struct(...
      'domain', [0.6, 0.95], ...
      'anchor', 'x2'), ...
    'xaxis2', struct(...
      'domain', [0.6, 0.95], ...
      'anchor', 'y2'));
response = plotly(data, struct('layout', layout, 'filename', 'simple-inset', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

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