首页 文章

matlab中的3个x轴图?

提问于
浏览
1

我需要绘制一个带有3个x轴的图形 . 每个轴通过数学公式链接到另一个轴 . 我想这样做是因为x值可以看作波长[nm],速度[m / s]或能量[eV],我希望读者不必在每个图上自己转换它 .

我在网上搜索过,只找到了something for 2 x-axes,但没有了 .

Edit :我使用的是版本R2011a .

所以它应该是这样的,我(显然)没有在MATLAB中创建:

What I 'd like to plot (it is not done on matlab this one...)

提前致谢!

2 回答

  • 3

    this answer所示,您可以创建一个高度接近零的新 axes 对象,因此它基本上只是x轴 . 请注意,所有实际绘图必须在第一个轴上完成,因为这是您可以看到的区域!

    演示代码:

    % Create some plotting data and plot
    x = 0:0.1:2*pi;   y = sin(x);
    % Plot, can specify line attributes (like LineWidth) either 
    % - inline: plot(x,y,'linewidth',2)
    % - after: p1 = plot(x,y); p1.LineWidth = 2;
    plot(x,y);
    % Get current axes object (just plotted on) and its position
    ax1 = gca;
    axPos = ax1.Position;
    % Change the position of ax1 to make room for extra axes
    % format is [left bottom width height], so moving up and making shorter here...
    ax1.Position = axPos + [0 0.3 0 -0.3];
    % Exactly the same as for plots (above), axes LineWidth can be changed inline or after
    ax1.LineWidth = 2;
    % Add two more axes objects, with small multiplier for height, and offset for bottom
    ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
    ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
    % You can change the limits of the new axes using XLim
    ax2.XLim = [0 10];
    ax3.XLim = [100 157];
    % You can label the axes using XLabel.String
    ax1.XLabel.String = 'Lambda [nm]';
    ax2.XLabel.String = 'Velocity [m/s]';
    ax3.XLabel.String = 'Energy [eV]';
    

    输出:

    multiple x axes matlab


    Edit
    2014b graphics changes之前,您需要进行一些调整以获取和设置轴属性 . 等效代码会更多地使用 set 命令,看起来像这样:

    x = 0:0.1:2*pi;   y = sin(x);
    plot(x,y);
    ax1 = findobj(gca, 'type', 'axes')
    axPos = get(ax1, 'Position');
    set(ax1, 'Position', axPos + [0 0.3 0 -0.3]);
    set(ax1, 'LineWidth', 2);
    ax2 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.15 0 0], 'color', 'none', 'linewidth', 2);
    ax3 = axes('position', (axPos .* [1 1 1 1e-3]) + [0 0.00 0 0], 'color', 'none', 'linewidth', 2);
    set(ax2, 'xlim', [0 10]);
    set(ax3, 'xlim', [100 157]);
    axes(ax1); xlabel('Lambda [nm]');
    axes(ax2); xlabel('Velocity [m/s]');
    axes(ax3); xlabel('Energy [eV]');
    
  • 1

    以下是如何执行此操作的示例:

    msx = [1 50 60 90];
    msy = [0 1 3 8];
    
    lx = 90/4*[1 2 3 4]; % Scale the data with respect to the data that will use the "primary" X-axis
    ly = [0 2 8 10];
    
    evx = 90/19*[1 7 10 19]; % Scale the data with respect to the data that will use the "primary" X-axis
    evy = [0 8 16 20];
    
    figure
    a=axes('units','normalized','position',[.1 .35 .7 .6],'xlim',[0 100],'xtick',0:10:100);
    plot(lx, ly)
    hold on
    plot(msx, msy)
    hold on
    plot(evx, evy)
    xlabel(a,'velocity m/s')
    b=axes('units','normalized','position',[.1 .21 .7 0.000001],'xlim',[0 4],'color','none', 'xtick',0:1:10);
    xlabel(b,'lambda nm');
    c=axes('units','normalized','position',[.1 .10 .7 0.000001],'xlim',[0 19],'color','none', 'xtick',0:1:19);
    xlabel(c,'energy eV');
    

    对于position:指定为[left bottom width height]形式的四元素向量 . 默认值[0 0 1 1]包括容器的整个内部 . (见https://de.mathworks.com/help/matlab/ref/axes-properties.html

    输出数字:
    enter image description here

相关问题