垂直偏移茎图
我如何垂直偏移stem
图,以使词干从y == 0.5而不是从x轴发出?
How do I vertically offset a stem
plot so that the stems emanate from say y == 0.5 instead of from the x-axis?
我知道我可以更改x-tick-marks,但是最好只更改绘图.
I know I could change the x-tick-marks but it would be better to rather just change the plot.
stem(X+0.5)
不起作用,因为它会使茎变长.
stem(X+0.5)
doesn't work as it will just make the stems longer.
我也有正面和负面的数据.而且我在同一轴上还有其他我不想偏移的图.
Also I have both positive and negative data. And also I have other plots on the same axis which I don't want to offset.
基于下面的路易斯·门多(Luis Mendo)的答案,我为此编写了一个函数(但是实际上,下面的我的答案无论如何都有一个内置的属性):
Based on Luis Mendo's answer below, I have written a function for this (however see my answer below as MATLAB actually has a built-in property for this anyway):
function stem_offset(x_data, y_data, offset, offset_mode, varargin)
%STEM_OFFSET stem plot in which the stems begin at a position vertically
%offset from the x-axis.
%
% STEM_OFFSET(Y, offset) is the same as stem(Y) but offsets all the lines
% by the amount in offset
%
% STEM_OFFSET(X, Y, offset) is the same as stem(X,Y) but offsets all the
% lines by the amount in offset
%
% STEM_OFFSET(X, Y, offset, offset_mode) offset_mode is a string
% specifying if the offset should effect only the base of the stems or
% also the ends. 'base' for just the base, 'all' for the baseand the
% ends. 'all' is set by default
%
% STEM_OFFSET(X, Y, offset, offset_mode, ...) lets call all the stem()
% options like colour and linewidth etc as you normally would with
% stem().
if nargin < 3
offset = 1:length(y_data);
y_data = x_data;
end
if nargin < 4
offset_mode = 'all';
end
h = stem(x_data, y_data, varargin{:});
ch = get(h,'Children');
%Offset the lines
y_lines = get(ch(1),'YData'); %// this contains y values of the lines
%Offset the ends
if strcmp(offset_mode, 'all')
set(ch(1),'YData',y_lines+offset)
y_ends = get(ch(2),'YData'); %// this contains y values of the ends
set(ch(2),'YData',y_ends+offset)
else
set(ch(1),'YData',y_lines+offset*(y_lines==0)) %// replace 0 (i.e. only the start of the lines) by offset
end
end
我现在已上传到文件交换中心( http ://www.mathworks.com/matlabcentral/fileexchange/45643-stem-plot-with-offset )
Which I have now uploaded to the file exchange (http://www.mathworks.com/matlabcentral/fileexchange/45643-stem-plot-with-offset)
看来stem
中确实有一个属性,为此我以某种方式错过了它!
It appears that there actually is a property in stem
for this that I somehow missed!
http://www.mathworks.com/help/matlab/ref/stem.html#btrw_xi-87
例如来自文档:
figure
X = linspace(0,2*pi,50)';
Y = (exp(0.3*X).*sin(3*X));
h = stem(X,Y);
set(h,'BaseValue',2);