How to plot two plotyy plots in the same plot?

A
angelinawilliams97 · Mar 16, 2020 · 2.2K views
Question
I am trying to plot four plots in the same figure. First i plot using plotyy. Then two graphs are created, each with a y-axis. Now I want to plot another two plots. One using y-axis-1 and one using y-axis-2. How do i do this? If I do   [ax,h1,h2] = plotyy(...) hold on plot(ax(1),...) plot(ax(2),...) i don't get all four plots.
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Sep 16, 2026





   

Add Marginal Histograms to a Scatter Plot in MATLAB

   

Method 1: Using Built-in scatterhist (Toolbox Method)


   

If you have the Statistics and Machine Learning Toolbox, scatterhist provides the fastest way to generate marginal histograms.

   

% Generate sample data
rng(42);
x = randn(500, 1);
y = 2*x + randn(500, 1);

% Create scatter plot with marginal histograms
figure('Color', 'w');
h = scatterhist(x, y, ...
    'Location', 'NorthEast', ...   % Positions: 'NorthEast', 'SouthEast', etc.
    'Direction', 'out', ...         % Histogram bars point outward
    'Color', [0.2 0.4 0.8], ...
    'Marker', 'o', ...
    'MarkerSize', 5);

% Apply axis labels to the main plot
xlabel(h(1), 'Variable X');
ylabel(h(1), 'Variable Y');
title(h(1), 'Scatter Plot with Marginal Histograms');

   

Grouped Data with Kernel Density Curves


   
group = [repmat({'Group A'}, 250, 1); repmat({'Group B'}, 250, 1)];
x = [randn(250, 1); randn(250, 1) + 2];
y = [randn(250, 1); randn(250, 1) - 1];

figure('Color', 'w');
scatterhist(x, y, ...
    'Group', group, ...
    'Kernel', 'on', ...             % Overlays smooth density estimates
    'Color', 'br', ...
    'Legend', true);

   

Method 2: Using tiledlayout (Base MATLAB, R2019b and Later)


   

This method requires no add-on toolboxes and gives you complete control over colors, bin widths, and spacing.

   

% Generate sample data
rng(10);
x = 3 + randn(1000, 1);
y = 5 + 1.5 * randn(1000, 1);

figure('Color', 'w', 'Position', [100, 100, 700, 700]);
t = tiledlayout(4, 4, 'TileSpacing', 'compact', 'Padding', 'compact');

% 1. Top marginal histogram (spans row 1, columns 1 to 3)
ax_top = nexttile(t, 1, [1, 3]);
histogram(ax_top, x, 25, 'FaceColor', [0.3 0.6 0.9], 'EdgeColor', 'w');
set(ax_top, 'XTickLabel', [], 'Box', 'off');
ylabel(ax_top, 'Count');
grid(ax_top, 'on');

% 2. Central scatter plot (spans rows 2 to 4, columns 1 to 3)
ax_main = nexttile(t, 5, [3, 3]);
scatter(ax_main, x, y, 20, [0.1 0.4 0.7], 'filled', 'MarkerFaceAlpha', 0.5);
xlabel(ax_main, 'X Axis');
ylabel(ax_main, 'Y Axis');
grid(ax_main, 'on');

% 3. Right marginal histogram (spans rows 2 to 4, column 4)
ax_right = nexttile(t, 8, [3, 1]);
histogram(ax_right, y, 25, ...
    'FaceColor', [0.9 0.4 0.3], ...
    'EdgeColor', 'w', ...
    'Orientation', 'horizontal');   % Rotates bars along the Y-axis
set(ax_right, 'YTickLabel', [], 'Box', 'off');
xlabel(ax_right, 'Count');
grid(ax_right, 'on');

% Synchronize axes limits
linkaxes([ax_main, ax_top], 'x');
linkaxes([ax_main, ax_right], 'y');

   

Method 3: Using Direct axes Positioning (Universal / R2019a Compatibility)


   

For earlier MATLAB versions, define normalized bounding boxes for each axis.

   

x = randn(600, 1);
y = 0.8 * x + randn(600, 1);

figure('Color', 'w', 'Position', [150, 150, 650, 650]);

% Main scatter plot [left, bottom, width, height]
ax_main = axes('Position', [0.12, 0.12, 0.60, 0.60]);
scatter(ax_main, x, y, 18, 'filled', 'MarkerFaceColor', [0.2 0.5 0.8]);
xlabel('X Variable');
ylabel('Y Variable');
grid on;

% Top histogram
ax_top = axes('Position', [0.12, 0.75, 0.60, 0.18]);
histogram(ax_top, x, 20, 'FaceColor', [0.2 0.5 0.8], 'EdgeColor', 'none');
set(ax_top, 'XTickLabel', [], 'Box', 'off');
ylabel('Count');

% Right histogram
ax_right = axes('Position', [0.75, 0.12, 0.18, 0.60]);
histogram(ax_right, y, 20, 'Orientation', 'horizontal', ...
    'FaceColor', [0.2 0.5 0.8], 'EdgeColor', 'none');
set(ax_right, 'YTickLabel', [], 'Box', 'off');
xlabel('Count');

% Keep axes synchronized
linkaxes([ax_main, ax_top], 'x');
linkaxes([ax_main, ax_right], 'y');

   


        Key Implementation Details:
       

               
  • 'Orientation', 'horizontal': Flips the side histogram bars horizontally so they align with the vertical Y-axis.

  •            
  • linkaxes(...): Locks the axes together so that zoom, pan, and manual limit changes stay in sync across the scatter plot and both histograms.

  •            
  • 'MarkerFaceAlpha', 0.5: Adds point transparency to make point density clear in overlapping areas.

  •        

   


100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!