stem plot with bars that don't go through zero

16 views (last 30 days)
I'm trying to achieve a plot that looks like this . The stem plot seems to be the one to use, with a Y input having two columns for the lower and upper bounds, however it seems the vertical lines always have to be connected to the horizontal (zero) line of the plot. any way to make them independent of that line, for instance go from 3 to 5 on the y axis? Thanks!

Accepted Answer

José-Luis
José-Luis on 5 Jul 2016
lower_lim = -2 * rand(1,15);
upper_lim = 2 * rand(1,15);
x_val = (1:15);
aH = axes;
hold on;
for ii = [lower_lim;upper_lim;x_val]
plot(aH,[ii(3), ii(3)],[ii(1), ii(2)], 'b-');
end
mean_lim = (mean(lower_lim) + mean(upper_lim))/2;
plot(aH,aH.XLim,[mean_lim, mean_lim], 'r--');

More Answers (2)

Steven Lord
Steven Lord on 5 Jul 2016
It sounds like either you want errorbars as created by the errorbar function or you want to change the baseline of your stem plot as per the last example on the stem documentation page.

Thorsten
Thorsten on 5 Jul 2016
You can use my function stem2 to do it:
X = linspace(0,2*pi,50)';
Y = [cos(X), 0.5*sin(X)];
h = stem2(X,Y);
set(h, 'Color', 'b', 'LineWidth', 2)
with stem2 given as
function h = stem2(x, y)
%STEM2 Stem plot with upper and lower bounds
%
% H = STEM2(X, Y)
%
% Thorsten.Hansen@psychol.uni-giessen.de 2016-07-05
if nargin == 0 % demo
X = linspace(0,2*pi,50)';
Y = [cos(X), 0.5*sin(X)];
h = stem2(X,Y);
set(h, 'Color', 'b', 'LineWidth', 2)
clear h
return
end
if nargin == 1
y = x;
end
% check that y is a N*2 column vector
if size(y,2) ~= 2,
y = y';
if size(y, 2) == 2
warning('Y should be a Nx2 column vector.')
else
error('Y should be a Nx2 column vector.')
end
end
N = size(y, 1);
% generate x if not given
if nargin == 1
x = 1:N;
end
x = x(:); % force column vector
x = [x x nan(N,1)]';
y = [y nan(N,1)]';
h = plot(x(:), y(:));
if nargout == 0
clear h
end

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!