Search code examples
matlabgraphing

2-d graph with two different axis showing different min and max MATLAB


I have the graph below I need to create two different x-axis. The unique part of this problem is where the min and max values need to be located. The range for is 0-100 for both, however the 100% value on the second x-axis needs to be where the 50% value is on the first. See the picture for clarification. The red is what I need to add using MATLAB.

I did a lot of looking and while it's very simple to put two different axis on one graph, I couldn't find a solution for this particular problem. I'd like this to be done in the code and not plot tools.

enter image description here


Solution

  • How about this

    % dummy data
    y = 1:80; 
    x1 = 100*sin( 4*pi*y/80 ).^2 ;  
    x2 = 100*cos( 5*pi*y/80).^2; 
    

    Plot the first line

    figure; 
    line( x1, y, 'Color', 'b', 'LineWidth', 2 );
    

    Get position and size of first plot

    haxes1 = gca; 
    haxes1_pos = get(haxes1,'Position');
    

    set the 100% of second plot to 50% of first ("tweaking" the width of the axis)

    haxes1_pos(3) = haxes1_pos(3)/2; 
    haxes2 = axes('Position',haxes1_pos,'XAxisLocation','top','Color','none','XColor','r');
    

    Plot the second line

    line( x2, y, 'Color', 'k', 'LineWidth',2,'Parent',haxes2);
    

    And this is what you get enter image description here