matplotlib:具有水平偏移的干图

matplotlib: stem plot with horizontal offset

我想在同一个图形上绘制 2 个主干图。 这是一个例子:

我从 matplotlib 中找到了这个干图示例: http://matplotlib.org/examples/pylab_examples/stem_plot.html

但是,我看不到如何向针状图添加偏移量。 (Y 轴上的 +1 或 +2)。

也许另一种情节类型也适合我?我想用竖线显示小事件。

此功能类似于 Matlab 柱状图中的 "BaseValue"。

您可以使用关键字 bottom

from pylab import *

x = linspace(0.1, 2*pi, 10)
markerline, stemlines, baseline = stem(x, cos(x), '-.', bottom=.5)
setp(markerline, 'markerfacecolor', 'b')
setp(baseline, 'color','r', 'linewidth', 2)

show()

在 Matlab 中使用 refline 作为解决方法,可以根据需要显示两个柱状图。

clear all;

x = 1:5;
y1 = [1, 0, -1, 0, 1];
y2 = [-.5, 1, .5, -1, 0];

b1 = 0; % base line for y1
b2 = -2; % base line for y2

y1 = y1 + b1;
y2 = y2 + b2;

y = [y1; y2]';

h = stem(x,y);

set(h(1), 'BaseValue' , b1);
set(h(2), 'BaseValue' , b2);

hold on;

refline(0,b1); % refline was used a workaround

axis([1 5 -5 5])

图: