Search code examples
matlabplotgraphmatlab-figure

How to keep scale constant on two different graphs (2D) in Matlab


I am plotting the deflections of a bar at two different lengths with both their experimental and theoretical values.

I have two graphs that I need to create, but the axis need to have the same scale. I am currently plotting the two graphs on the same graph, but it looks cluttered and the report needs the second graph. I want it to be obvious to see the difference in the deflection of a bar on its edge and on its flat side.

Current Graph and Code:

plot(L_4F,Load_Flat,'DisplayName','Flat: L/4 Exp','LineWidth', 1)

hold on

plot(y_L_4F,Load_Flat,'DisplayName','Flat: L/4 Theo','LineWidth', 1)

hold on

plot(L_2F,Load_Flat,'DisplayName','Flat: L/2 Exp','LineWidth', 1)

hold on

plot(y_L_2F,Load_Flat,'DisplayName','Flat: L/2 Theo','LineWidth', 1)

hold on



%% Plotting L/2

plot(L_4E,Load_Edge,'DisplayName','Edge: L/4 Exp','LineWidth', 1)

hold on

plot(y_L_4E,Load_Edge,'DisplayName','Edge: L/4 Theo','LineWidth', 1)

hold on

plot(L_2E,Load_Edge,'DisplayName','Edge: L/2 Exp','LineWidth', 1)

hold on

plot(y_L_2E,Load_Edge,'DisplayName','Edge: L/2 Theo','LineWidth', 1)

ldg = legend('Show');
ldg.Location = 'Best';

want to show the graphical difference but on separate graphs Graph


Solution

  • You can use the linkaxes function. Documented here

    Example using 2 subplots on 1 figure with only linking in the X-direction:

    figure
    ax1 = subplot(2,1,1);plot(sin(0:.1:2*pi));
    ax2 = subplot(2,1,2);plot(cos(0:.1:2*pi));
    linkaxes([ax1,ax2],'x');
    

    or x and y directions

    linkaxes([ax1,ax2],'xy');
    

    Another Example of linking between 2 separate figures:

    figure
    plot(sin(0:.1:2*pi));
    ax1 = gca;
    
    figure;
    plot(cos(0:.1:2*pi));
    ax2 = gca;
    linkaxes([ax1,ax2],'xy');