Python 将值放在柱状图的箱子中心

Python 将值放在柱状图的箱子中心,python,matplotlib,histogram,Python,Matplotlib,Histogram,我有下面的代码来绘制直方图。time\u 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_lis

我有下面的代码来绘制直方图。
time\u 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()
这会生成一个直方图,但是这些箱子并没有像我希望的那样对齐。我想把时间放在垃圾桶的中心,而不是边缘

我提到过,但它似乎也没有回答这个问题

我尝试使用以下代码绘制直方图,但它没有为值
23

plt.hist(hour_list, bins=np.arange(24)-0.5)


谁能帮我得到24个箱子,每个箱子的中心是小时吗?

要得到24个箱子,您需要在定义箱子边缘的序列中有25个值。对于
n
bin,始终存在
n+1

所以,改变你的路线

plt.hist(hour_list,bins=np.arange(24)-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()
结果:


另外一个问题,只是为了使解决方案普遍适用。几个月来,我一直在尝试做类似的事情。因此,我希望x轴由字符串而不是数字组成。根据您的解决方案
plt.hist(hour_list,bins=np.arange(13)-0.5)
我将获得居中编号。但是如何将像
['Jan','Feb'…]这样的字符串居中放置。@AbhishekBhatia您可以更改
xticklabels
将数字更改为strings@JRichardSnape你能在解决方案中提供一个例子吗。我试过
plt.xticks(np.arange(13)-0.5,label)plt.hist(hour\u list,bins=np.arange(13)-0.5)
,但它不正确('centered')。我没有得到你想要的。如果您有24个值和12个存储箱,则每个存储箱将包含2个值。你为什么从25岁改到13岁?为什么我们要带几个月?你错过了网站的模型-我们不能在评论中添加越来越多的问题。正如@tom所说,如果您想要任意标签文本,请更改
xticklabels
。您可以在谷歌上搜索“matplotlib set xtick labels”,得到第一个结果,这就解释了it@AbhishekBhatia您的标签数量是否与箱子数量匹配?我敢打赌,你有25个标签的24箱。。。如果第一个箱子的中心需要
0
,最后一个箱子的中心需要
24
,则可能需要
bins=np.arange(26)-0.5
。请注意,您有25个存储箱,您的
0
24
存储箱不明确-发生在午夜的事件进入哪个存储箱?
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()