How to insert two X axis in a Matlab a plot

后端 未结 3 2118
悲哀的现实
悲哀的现实 2020-12-01 12:51

I would like create a Matlab figure with a double X axis (m/s and km/h) with the same plot.

I have found plotyy and - in Matlab reposity - plotyyy, but I am looking

3条回答
  •  半阙折子戏
    2020-12-01 13:25

    As a very simple alternative you could also create a 2nd axis (transparent) and put it below the first one so that you only see the x axis.

    Example:

    clear
    clc
    close all
    
    x = 1:10;
    
    x2 = x/3.6;
    
    y = rand(size(x));
    
    hP1 = plot(x,y);
    
    a1Pos = get(gca,'Position');
    
    %// Place axis 2 below the 1st.
    ax2 = axes('Position',[a1Pos(1) a1Pos(2)-.05 a1Pos(3) a1Pos(4)],'Color','none','YTick',[],'YTickLabel',[]);
    
    %// Adjust limits
    xlim([min(x2(:)) max(x2(:))])
    
    text(2.85,0 ,'m/s','FontSize',14,'Color','r')
    text(2.85,.05 ,'km/h','FontSize',14,'Color','r')
    

    Output:

    enter image description here

    Then you can manually add the x labels for each unit, in different color for example.

提交回复
热议问题