本文介绍了在给定bin端点和值的情况下绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个bin边数组和一个bin值数组. (基本上是plt.hist
的输出).例如:
Say I have an array of bin edges, and an array of bin values. (basically the output of plt.hist
). For instance:
bins = np.array([1, 2, 3, 4, 5])
vals = np.array([2, 5, 5, 2])
如何将其绘制为直方图?
How do I plot this as a histogram?
为了清楚起见,我的意思是vals是每个bin的高度",其中len(vals)+1 = len(bins)
for clarity, I mean vals to be the "height" of each bin, where len(vals) + 1 = len(bins)
推荐答案
如果使用的是python 3.5
,则可以使用pyplot
fill_between
函数.您可以使用以下代码:
If you are using python 3.5
you can use pyplot
fill_between
function for such. You can use following code:
import numpy as np
import matplotlib.pyplot as plt
bins = np.array([1, 2, 3, 4, 5])
vals = np.array([2, 5, 5, 2])
plt.fill_between(bins,np.concatenate(([0],vals)), step="pre")
plt.show()
这将生成以下图形:
This will generate below graph:
这篇关于在给定bin端点和值的情况下绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!