Skip to content

Instantly share code, notes, and snippets.

@mnguyenngo
Last active September 12, 2018 19:39
Show Gist options
  • Save mnguyenngo/1a9a5a76bf6b1f8885ec3ee77885ce2a to your computer and use it in GitHub Desktop.
Save mnguyenngo/1a9a5a76bf6b1f8885ec3ee77885ce2a to your computer and use it in GitHub Desktop.
Centering histogram bar plots
"""Histogram bars are not centered at their label by default.
The function below returns the bins parameter value for use with matplotlib's
hist() method.
"""
import numpy as np
import pandas as pd
def bin_array(distribution):
"""Returns an array to be used as the parameter value to center the
histogram bars at the corresponding label.
Arguments:
distribution (np arr or pd series)
Returns:
bins (np arr)
"""
uniq = distribution.unique() # array of unique values
uniq = uniq[~np.isnan(uniq)] # remove nan values
num_unique = len(uniq)
uniq_min = min(uniq)
bins = np.arange(uniq_min, num_unique + 1 + uniq_min) - 0.5
return bins # [0.5, 1.5, 2.5, 3.5] if there are 3 unique categories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment