首页 > 解决方案 > 我无法在条形尖端上方绘制值,XEndPoints 不起作用

问题描述

我正在使用Matlab 2018b,我想在条的提示上方绘制值,其中

A=[4, 8 , 20 ,5];
B=[1,3 4 ,8 ];y

我想在条形上方写字符串,但是当我使用时XEndPoints,会显示以下消息:

No appropriate method, property, or field 'XEndPoints' for class 'matlab.graphics.chart.primitive.Bar'.

我的代码:

function DisplayBarTopsisSawRankReversal(A,B,...  
                                             X1,X2)
    
    max1= max(A(1,:));
    max2=max(B(1,:));
    MAX=max(max1,max2);
    % This will produce 4 groups of 3 different colored bars
    x = [1 2 ];
    
    
    y = [A;B];
    Bar = bar(x,y);  % h will have 3 handles, one for each color of bars
   
    
    set(Bar, {'DisplayName'}, {'Txt1','Txt2'}');

    legend() ;
    
    
    
    somenames={X1,X2};
    set(gca,'xticklabel',somenames);
    ylabel('Rank Reversal Ratio [%]')
    
    ylim([0 MAX+30]);
    
    
    %text(xtips1,ytips1,string( ) ,'HorizontalAlignment','center','VerticalAlignment','bottom')
    opts = {'VerticalAlign','middle', 'HorizontalAlign','left', ...
        'FontSize',8, 'Rotation',90};
    
    text(1, A(1) ,'s' , opts{:});
    numel(Bar)
    
    xtips2 = b(2).XEndPoints;

    end

如何在我的情况下添加字符串?

标签: matlabplot

解决方案


'XEndPoints'是 Matlab R2019b 中的一个新功能。如果您尝试绘制单个条形图,这可能会对您有所帮助:

bar(data);
for i = 1:length(data)
    text(i, data(i), num2str(data(i), '%g'),...
    'HorizontalAlignment','center','VerticalAlignment','bottom');
end

当您决定绘制分组条形图时,最好将您的 Matlab 更新到 R2019b。

还有另一种方法可以解决这个问题:使用函数“rectangle”设计自己的函数“MyBar”。


推荐阅读