x[0::2], x[1::2] = bins, bins
x -= 0.5*(bins[1]-bins[0])
设置每个条的高度,成对但偏移1(相对于x值),以产生阶跃效果
# n is an array of arrays containing the number of items per bar
patches = [] # from line 7676
for m, c in zip(n, color):
y[1:-1:2], y[2::2] = m, m
patches.append(self.fill(x, y, closed=False, edgecolor=c, fill=False))
from matplotlib import pyplot
import numpy as np
#sample data:
x = np.arange(30)
y = np.cumsum(np.arange(30))
#offset the x for horizontal, repeat the y for vertical:
x = np.ravel(zip(x,x+1))
y = np.ravel(zip(y,y))
pyplot.plot(x,y)
pyplot.savefig('plt.png')
def plot_binned_data(axes, binedges, data,
*args, **kwargs):
#The dataset values are the bin centres
x = (binedges[1:] + binedges[:-1]) / 2.0
#The weights are the y-values of the input binned data
weights = data
return axes.hist(x, bins=binedges, weights=weights,
*args, **kwargs)
从http://matplotlib.sourceforge.net/examples/pylab_examples/histogram_demo_extended.html的附带来源
下面是他们如何绘制的图表:
[剪]
你想要的是
编辑:如果您想知道hist()是如何工作的,请查看源代码-它是在matplotlib/axes.py中从第7407行开始定义的。
看7724号线
对于N个条,bins是N+1值的numpy.ndarray,是每个条的边。它们将每个条的值双倍化(这是fraxel对下面的np.ravel所做的),并将数据点左移半条使其居中
设置每个条的高度,成对但偏移1(相对于x值),以产生阶跃效果
而
self.fill
位实际上是画线的。您可以通过偏移数据并使用
plot
来作弊:情节:
最简单的解决方案是将已装箱的数据集转换为未装箱的加权数据集(元素数=箱数)。未绑定的数据集将由等于bin中心的数据值和等于每个bin中的值的权重组成。例如,假设你的binned数据是
相应的加权数据集是
请注意,使用bin中心的选择是任意的,可以使用bin中的任何点。一旦生成了未装箱的数据集,就可以使用标准的matplotlib直方图绘制(即Axes.hist)。
python中的一个示例实现如下:
现在您可以完全访问所有Axes.Histogram绘图选项,包括
histtype="step"
来创建所需的阶梯直方图。使用这个函数的一个例子是
希望能有帮助!
相关问题 更多 >
编程相关推荐