【发布时间】:2017-11-01 17:14:44
【问题描述】:
我的数据包含各种波动率值,即 0 到 1 之间的十进制数。现在,只有 6% 到 24% 之间的值特别相关,因此我正在尝试构建一个直方图来显示相对这些值的计数。我想要一个包含 0-6%、6-8%、... 22-24%、>24% 的直方图,但事实证明这非常困难。总共应该有 11 个 bin,并且 bin 的标签应该在 bin 下方居中。 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()
这会生成几乎正确的直方图,但 bin 不在我想要的间隔上,xticks 没有居中,并且每个 bin 没有一个标签。
【问题讨论】:
-
你需要展示一些代码并解释输出与你想要的有什么不同。
-
我认为我使用的代码与我真正想要的相差太远。我主动选择不发布代码,因为我不希望有人使用我已经使用过的东西。如果您真的认为我应该包含我的代码,我会发布它,但我想要一个全新的解决方案,而不是对我已经拥有的东西进行调整。我不喜欢我使用的方法,新的解决方案只能比我现有的更好
-
不,您不是要发布您的代码,而是要发布问题的minimal reproducible example。
-
能否请您发布您的python版本和系统?
-
我有 3.6.1 版,在 Windows 7 机器上
标签: python matplotlib histogram