【问题标题】:3 x-axis in matlab plot?matlab图中的3个x轴?
【发布时间】:2017-07-17 07:55:14
【问题描述】:

我需要用 3 个 x 轴绘制一个图形。每个轴通过数学公式相互链接。我想这样做是因为 x 值可以被视为波长 [nm]、速度 [m/s] 或能量 [eV],我希望读者不必在每个图表上自行转换它。

我在网上搜索,只找到something for 2 x-axes,但没有更多。

编辑:我使用的是 R2011a 版本。

所以它应该看起来像这样,我(显然)不是在 MATLAB 中创建的:

提前致谢!

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案1】:

    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]';
    

    输出:


    编辑
    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]');
    

    【讨论】:

    • 感谢您的回答!它运作良好:) 为了增加线宽,它是 ax1.LineWidth =.... ?
    • 绘制线的线宽?请查看我在代码中添加的其他 cmets。
    • 不,轴的宽度。我喜欢它厚的时候!通常我使用这个:set(gca, 'FontSize', 13, 'fontName','calibri','LineWidth',2.0,'FontWeight','bold'); 所以现在我必须将 gca 更改为 ax1、ax2 和 ax3 吗? (我现在无法尝试,我无法访问我的计算机)
    • 啊,是的,轴的线宽可以和绘图线宽一样改变,再次请看我的编辑演示 2 方法,或者你可以在评论中使用set
    • 如果您可以再次编辑您的代码:在重新编译程序之前,您需要一个close all 和一个clear。如果没有这个,它会发送错误消息,因为宽度或高度是负数。
    【解决方案2】:

    这里有一个例子来说明如何做到这一点:

    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');
    

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

    输出图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      相关资源
      最近更新 更多