我的数据包含各种波动率值,即0到1之间的十进制数。现在,只有6%到24%之间的值特别相关,因此我正在尝试构建直方图以显示相对值这些值的计数。我想要一个直方图,其具有0-6%,6-8%,...... 22-24%,> 24%的区间,但这证明是非常困难的。总共应该有11个箱子,箱子的标签应该放在箱子的中央。 y轴不能有任何标签,因为只有相对计数很重要,包括y轴上的值会减损我试图强调的相对性。
通过阅读Matplotlib xticks not lining up with histogram和Bin size in Matplotlib (Histogram)等答案,我已经疯狂了。
如果有人能帮我解决这个问题,我将非常感激。谢谢你的帮助。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
graph1, ax = plt.subplots()
n, bins = np.histogram(values, 11)
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T
barpath = path.Path.make_compound_path_from_polys(XY)
patch = patches.PathPatch(barpath, facecolor="blue", edgecolor="#FFFFFF",
alpha = 0.75)
ax.add_patch(patch)
ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max())
graph1.suptitle("1-Year Rolling Volatilities")
ax.axes.get_yaxis().set_visible(False)
plt.show()
这会产生几乎正确的直方图,但是这些区域不在我想要的区间,xticks不居中,并且每个区域没有一个标签。
答案 0 :(得分:0)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
graph1, ax = plt.subplots()
values = [0.15, 0.23, 0.061, 0.085, 0.06, 0.09, 0.07, 0.05, 0.04, 0.08]
#n, bins = np.histogram(values, 11)
plt.hist(values, bins=[0, 0.06, 0.08, 0.22, 0.24, 1.00])
你也可以使用这样的东西来设置你的垃圾箱下面的标记。有关在此堆栈溢出帖Matplotlib - label each bin
中设置滴答的更多信息