For example, how can I increase the width of each of the error bars in the following plot:
figure; X = 0:pi/10:pi; Y = sin(X); E = std(Y)*ones(size(X)); errorbar(X,Y,E);
In MATLAB R2016b, this can be accomplished using the ‘CapSize’ property of the Errorbar plot. The following code illustrates how to do this:
x = 0:pi/10:pi; y = sin(x); e = std(y)*ones(size(x)); figure h = errorbar(x,y,e); h.CapSize = 12;
In MATLAB versions R2014b through R2016a, this functionality is not present.
In MATLAB R2014a and earlier, you can change the width of these horizontal lines by modifying the ‘Xdata’ of each of them. The following code illustrates how to do this in an automated fashion:
hf = figure; X = 0:pi/10:pi; Y = sin(X); E = std(Y)*ones(size(X)); ha = errorbar(X,Y,E); hb = get(ha,'children'); Xdata = get(hb(2),'Xdata'); temp = 4:3:length(Xdata); temp(3:3:end) = ; % xleft and xright contain the indices of the left and right % endpoints of the horizontal lines xleft = temp; xright = temp+1; % Increase line length by 0.2 units Xdata(xleft) = Xdata(xleft) - .1; Xdata(xright) = Xdata(xright) + .1; set(hb(2),'Xdata',Xdata)
Below is a method I just found through trial and error (haha) that works for 2015b (I suspect it would also work for 2014b, 2015a, and 2016a, but I haven't tested it on these versions).
This method works by accessing a hidden primitive object type called LineStrip, whose handle is stored in a hidden property called Bar. The LineStrip object contains a property called VertexData, which stores coordinates for the endpoints (vertices) of all line segments used to draw the figure.
VertexData is a 3-by-(6*N) single-precision numeric array, where N equals the number of error bars represented by the ErrorBar object. This is because there are two vertices per line segment (start and end coordinates), and there are 3 lines that make up a single error bar (one vertical and two horizontal caps), so 2*3=6. More info:
You can then replace or alter these values as you wish to fully customize the error bars. An example implementation is shown here:
% Create errorbar to start with X = 0:pi/10:pi; Y = sin(X); E = std(Y)*ones(size(X)); hErr = errorbar(X,Y,E);
% Make cap-lines twice as long mult = 2; % twice as long b = hErr.Bar; % hidden property/handle drawnow % populate b's properties vd = b.VertexData; N = numel(X); % number of error bars capLength = vd(1,2*N+2,1) - vd(1,1,1); % assumes equal length on all newLength = capLength * mult; leftInds = N*2+1:2:N*6; rightInds = N*2+2:2:N*6; vd(1,leftInds,1) = [X-newLength, X-newLength]; vd(1,rightInds,1) = [X+newLength, X+newLength]; b.VertexData = vd;
(Note: The drawnow command is superfluous if you're stepping through in debug mode or using command-line entry, but it is absolutely essential if you're running in a function or script - otherwise the properties of the LineStrip will still be empty/default when you try to access them.)
Finally, of course, you could also set mult to 0 to eliminate the caps altogether, or manually put in the length you want, or anything else.
The above code works on Matlab 2013b, but does not work with Matlab 2015b. Could anyone suggest an alternative? Thanks.