在 Matlab 中调整误差条宽度

Adjust error bar width in Matlab

这是我的问题:

我有一个带 errorbar 的 MATLAB 图(一切正常),但条形图的宽度太宽了。有没有办法设置条的宽度?

如果你仔细看这张图片,你会看到几条红色和蓝色的线条,它们的大小符合我的要求(例如,w = 0.25)。

感谢任何帮助。

您需要访问他们的 XData 属性 并修改它们。查看 here 以获取 The Mathworks 的示例。

具体做法如下:

生成 errorbar 图:

hf = figure;
X = 0:pi/10:pi;
Y = sin(X);
E = std(Y)*ones(size(X));

hErrBar = errorbar(X,Y,E);

获取 XData 属性 以及 left/right 代表误差条水平线的索引。

hb = get(hErrBar,'children');  
Xdata = get(hb(2),'Xdata');

temp = 4:3:length(Xdata);
temp(3:3:end) = [];

xleft = temp; xright = temp+1;

根据需要修改数据并更新绘图。例如,将行长度减少 0.2 个单位

Xdata(xleft) = Xdata(xleft) + .1;
Xdata(xright) = Xdata(xright) - .1;

%// Update
set(hb(2),'Xdata',Xdata)

例如,

之前:

之后:

我找到了修改错误栏宽度的代码。

它的使用非常简单。绘制误差线后:

h = errorbar(X, Y, L, U, ...);

您必须调用函数:

errorbar_tick(h,w);

如评论中所述。

密码是:

function errorbar_tick(h,w,xtype)
%ERRORBAR_TICK Adjust the width of errorbars
%   ERRORBAR_TICK(H) adjust the width of error bars with handle H.
%      Error bars width is given as a ratio of X axis length (1/80).
%   ERRORBAR_TICK(H,W) adjust the width of error bars with handle H.
%      The input W is given as a ratio of X axis length (1/W). The result 
%      is independent of the x-axis units. A ratio between 20 and 80 is usually fine.
%   ERRORBAR_TICK(H,W,'UNITS') adjust the width of error bars with handle H.
%      The input W is given in the units of the current x-axis.
%
%   See also ERRORBAR
%
% Author: Arnaud Laurent
% Creation : Jan 29th 2009
% MATLAB version: R2007a
%
% Notes: This function was created from a post on the french forum :
% http://www.developpez.net/forums/f148/environnements-developpement/matlab/
% Author : Jerome Briot (Dut) 
%   http://www.mathworks.com/matlabcentral/newsreader/author/94805
%   http://www.developpez.net/forums/u125006/dut/
% It was further modified by Arnaud Laurent and Jerome Briot.

% Check numbers of arguments
error(nargchk(1,3,nargin))

% Check for the use of V6 flag ( even if it is depreciated ;) )
flagtype = get(h,'type');

% Check number of arguments and provide missing values
if nargin==1
    w = 80;
end

if nargin<3
   xtype = 'ratio';
end

% Calculate width of error bars
if ~strcmpi(xtype,'units')
    dx = diff(get(gca,'XLim')); % Retrieve x limits from current axis
    w = dx/w;                   % Errorbar width
end

% Plot error bars
if strcmpi(flagtype,'hggroup') % ERRORBAR(...)

    hh=get(h,'children');       % Retrieve info from errorbar plot
    x = get(hh(2),'xdata');     % Get xdata from errorbar plot

    x(4:9:end) = x(1:9:end)-w/2;    % Change xdata with respect to ratio
    x(7:9:end) = x(1:9:end)-w/2;
    x(5:9:end) = x(1:9:end)+w/2;
    x(8:9:end) = x(1:9:end)+w/2;

    set(hh(2),'xdata',x(:)) % Change error bars on the figure

else  % ERRORBAR('V6',...)

    x = get(h(1),'xdata');      % Get xdata from errorbar plot

    x(4:9:end) = x(1:9:end)-w/2;    % Change xdata with respect to the chosen ratio
    x(7:9:end) = x(1:9:end)-w/2;
    x(5:9:end) = x(1:9:end)+w/2;
    x(8:9:end) = x(1:9:end)+w/2;

    set(h(1),'xdata',x(:))  % Change error bars on the figure

end