stem plot with bars that don't go through zero
Show older comments
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
More Answers (2)
Steven Lord
on 5 Jul 2016
2 votes
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
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!