Put value at centre of bins for histogram

2024/10/9 2:27:15

I have the following code to plot a histogram. The values in time_new are the hours when something occurred.

    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_newprint hour_listnumbers=[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()

This produces a histogram, but the bins are not aligned as I would like. I want the hour to be in the centre of the bin, not on the edge.

Histogram of time_new with default bins

I referred to this question / answer, but it seems not to answer the question either.

I tried the following code for the histogram plot instead, but it didn't plot a bar for the value 23

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

histogram with bin range specified

Can anyone help me to get 24 bins, with the hour at the centre of each?

Answer

To get 24 bins, you need 25 values in your sequence defining bin edges. There are always n+1 edges for n bins.

So, alter your line

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

to

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

Note - your test data should have both edge cases in it. If you are simply extracting hours by rounding, there should be some 0 values in the list.


Full example:

import matplotlib.pyplot as plt
import numpy as npdef plot_my_time_based_histogram():#Note - changed the 24 values for 0time_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_newprint hour_listnumbers=[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 barplt.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()

Result:

histogram with centred bins

https://en.xdnf.cn/q/70065.html

Related Q&A

plot in Pandas immediately closes

I have a problem of plotting the data. I run the following python code:import pandas as pd df = pd.read_csv("table.csv")values = df["blah"] values.plot() print 1df[blahblah].plot() …

Django template: Embed css from file

Im working on an email template, therefor I would like to embed a css file<head><style>{{ embed css/TEST.css content here }}</style> </head>instead of linking it<head><…

handling async streaming request in grpc python

I am trying to understand how to handle a grpc api with bidirectional streaming (using the Python API).Say I have the following simple server definition:syntax = "proto3"; package simple;serv…

Add new column to a HuggingFace dataset

In the dataset I have 5000000 rows, I would like to add a column called embeddings to my dataset. dataset = dataset.add_column(embeddings, embeddings) The variable embeddings is a numpy memmap array of…

Django: how to order_by on a related field of a related field

Im using annotate to add a property to an object which I can then use for order_by. However, I want to annotate on a field of a relation on a relation. I know I should be able to get to the field someh…

How to extract the cell state and hidden state from an RNN model in tensorflow?

I am new to TensorFlow and have difficulties understanding the RNN module. I am trying to extract hidden/cell states from an LSTM. For my code, I am using the implementation from https://github.com/ay…

Python - Nested List to Tab Delimited File?

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g.,nested_list = [[x, y, z], [a, b, c]].I wish to create a function in order to output this data construct into a tab delim…

How to make sure buildout doesnt use the already installed packages?

I am trying to switch fully to buildout - but our development environment already has lot of stuff installed in /usr/lib/pythonxx/How can I make sure that buildout doesnt use the libraries installed on…

Can python setup.py install use wheels?

I am using setuptools. Is there a way to have the following command use wheels instead of source?python setup.py installIn particular, I have a custom package that requires pandas. While pandas insta…

Getting the last element of a level in a multiindex

I have a dataframe in this format:a b x 1 1 31 1 2 1 1 3 42 1 4 423 1 5 42 1 6 3 1 7 44 1 8 65437 1 9 73 2 1 5656 2 2 7 2 3 5 2 4 5 2 5 34a a…