【问题标题】:Matlab Bar Plot with multiple X-Axis具有多个 X 轴的 Matlab 条形图
【发布时间】:2018-07-11 05:57:58
【问题描述】:

我已经为此苦苦挣扎了一段时间。

我希望能够在使用条形图时绘制以下示例 except

simple plot that needs to be converted to bar plot

这是我自己实现的。这基本上是两个图相互重叠,每个轴都有不同的轴(当前示例显示相同的刻度值,但我希望它们稍后更改)

我的问题是使用条形图会像这样更改轴位置,我无法解决它。见下图:

desired image but lower axis are not aligned

这里的轴没有对齐。我需要两个都从同一个地方开始,也就是最左边的栏

非常感谢您的帮助 我的代码如下:

close all
figure
y= magic(10)
a=axes('Position',[.1 .2 .8 .7],'XColor','b');
%                 hold on;
bar( y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
xticklabels( {3:12});
legend;


hold on
bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
b= axes('Position',[.1 .1 .8 1e-12],'XColor','r')
xticklabels( {4:13});
legend;

编辑: 设法解决它 - 请参阅下面的更新代码

close all
figure
y= magic(10)
ax1=axes('Position',[.1 .2 .8 .7],'XColor','b');
bar(  y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
ax = gca
xticklabels( {3:12});
legend;


hold on
bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
axPos = ax1.Position;
ax2 = axes('Position', (axPos .* [1 1 1 1e-3])-[0 .07 0 -1e-3]  , 'XColor', 'r', 'linewidth', 2);

 set(ax2,'XLim',[-0.2 11.2])
set(ax2,'XTick',(1:10))
 xticklabels( {4:13});

【问题讨论】:

    标签: matlab user-interface bar-chart


    【解决方案1】:

    只需对两个条形图使用相同的 x 限制。 MATLAB 函数是“xlim”。

    figure
    y= magic(10)
    a=axes('Position',[.1 .2 .8 .7],'XColor','b'); hold all;
    bar( y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
    xticks(3:1:12); xlim([0 15]) 
    legend;
    
    bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
    b= axes('Position',[.1 .1 .8 1e-12],'XColor','r'); xlim([3 12]);
    xticks(4:1:13); xlim([0 15]) 
    legend;
    

    【讨论】:

    • 设法解决了这个问题。谢谢你!我已经用更新的代码更新了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2018-07-28
    • 2018-11-25
    相关资源
    最近更新 更多