将值放在直方图的 bin 中心
Put value at centre of bins for histogram
我有以下代码来绘制直方图。 time_new
中的值是某事发生的时间。
time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 14, 9, 10, 23, 19, 23, 20, 19, 6, 5, 24, 20, 19, 15, 14, 19, 14, 15, 21]
hour_list = time_new
print hour_list
numbers=[x for x in xrange(0,24)]
labels=map(lambda x: str(x), numbers)
plt.xticks(numbers, labels)
plt.xlim(0,24)
pdb.set_trace()
plt.hist(hour_list,bins=24)
plt.show()
这会生成一个直方图,但 bin 没有按我希望的那样对齐。我希望小时位于垃圾箱的中央,而不是边缘。
我提到了this question / answer,不过好像也没有回答问题
我尝试使用以下代码绘制直方图,但它没有为值 23
绘制条形图
plt.hist(hour_list, bins=np.arange(24)-0.5)
谁能帮我弄到 24 个垃圾箱,每个垃圾箱的中心是小时吗?
要获得 24 个 bin,您需要在序列中定义 bin 边缘的 25 个值。 n
个 bin 总是有 n+1
个边。
所以,改变你的台词
plt.hist(hour_list,bins=np.arange(24)-0.5)
到
plt.hist(hour_list,bins=np.arange(25)-0.5)
注意 - 您的测试数据应该包含两种极端情况。如果您只是简单地通过四舍五入提取小时数,列表中应该有一些 0
值。
完整示例:
import matplotlib.pyplot as plt
import numpy as np
def plot_my_time_based_histogram():
#Note - changed the 24 values for 0
time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 14, 9, 10, 23, 19, 23, 20, 19, 6, 5, 0, 20, 19, 15, 14, 19, 14, 15, 21]
fig, ax = plt.subplots()
hour_list = time_new
print hour_list
numbers=[x for x in xrange(0,24)]
labels=map(lambda x: str(x), numbers)
plt.xticks(numbers, labels)
#Make limit slightly lower to accommodate width of 0:00 bar
plt.xlim(-0.5,24)
plt.hist(hour_list,bins=np.arange(25)-0.5)
# Further to comments, OP wants arbitrary labels too.
labels=[str(t)+':00' for t in range(24)]
ax.set_xticklabels(labels)
plt.show()
plot_my_time_based_histogram()
结果:
我有以下代码来绘制直方图。 time_new
中的值是某事发生的时间。
time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 14, 9, 10, 23, 19, 23, 20, 19, 6, 5, 24, 20, 19, 15, 14, 19, 14, 15, 21]
hour_list = time_new
print hour_list
numbers=[x for x in xrange(0,24)]
labels=map(lambda x: str(x), numbers)
plt.xticks(numbers, labels)
plt.xlim(0,24)
pdb.set_trace()
plt.hist(hour_list,bins=24)
plt.show()
这会生成一个直方图,但 bin 没有按我希望的那样对齐。我希望小时位于垃圾箱的中央,而不是边缘。
我提到了this question / answer,不过好像也没有回答问题
我尝试使用以下代码绘制直方图,但它没有为值 23
plt.hist(hour_list, bins=np.arange(24)-0.5)
谁能帮我弄到 24 个垃圾箱,每个垃圾箱的中心是小时吗?
要获得 24 个 bin,您需要在序列中定义 bin 边缘的 25 个值。 n
个 bin 总是有 n+1
个边。
所以,改变你的台词
plt.hist(hour_list,bins=np.arange(24)-0.5)
到
plt.hist(hour_list,bins=np.arange(25)-0.5)
注意 - 您的测试数据应该包含两种极端情况。如果您只是简单地通过四舍五入提取小时数,列表中应该有一些 0
值。
完整示例:
import matplotlib.pyplot as plt
import numpy as np
def plot_my_time_based_histogram():
#Note - changed the 24 values for 0
time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 14, 9, 10, 23, 19, 23, 20, 19, 6, 5, 0, 20, 19, 15, 14, 19, 14, 15, 21]
fig, ax = plt.subplots()
hour_list = time_new
print hour_list
numbers=[x for x in xrange(0,24)]
labels=map(lambda x: str(x), numbers)
plt.xticks(numbers, labels)
#Make limit slightly lower to accommodate width of 0:00 bar
plt.xlim(-0.5,24)
plt.hist(hour_list,bins=np.arange(25)-0.5)
# Further to comments, OP wants arbitrary labels too.
labels=[str(t)+':00' for t in range(24)]
ax.set_xticklabels(labels)
plt.show()
plot_my_time_based_histogram()
结果: