stem plot with bars that don't go through zero
조회 수: 17 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
José-Luis
2016년 7월 5일
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--');
댓글 수: 0
추가 답변 (2개)
Steven Lord
2016년 7월 5일
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.
댓글 수: 0
Thorsten
2016년 7월 5일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!