Skip to content

Instantly share code, notes, and snippets.

@joezuntz
Created September 29, 2014 13:52
Show Gist options
  • Save joezuntz/2f3bdc2ab0ea59229907 to your computer and use it in GitHub Desktop.
Save joezuntz/2f3bdc2ab0ea59229907 to your computer and use it in GitHub Desktop.
Plot a numpy histogram vertically in text
import numpy as np
def ascii_hist(x, bins):
N,X = np.histogram(x, bins=bins)
total = 1.0*len(x)
width = 50
nmax = N.max()
for (xi, n) in zip(X,N):
bar = '#'*int(n*1.0*width/nmax)
xi = '{0: <8.4g}'.format(xi).ljust(10)
print '{0}| {1}'.format(xi,bar)
@mabrowning
Copy link

mabrowning commented Feb 17, 2023

Python3, with fixed indentation, and default for bins (because this is a top google result for "numpy ascii histogram")

def ascii_hist(x, bins=10):
    N,X = np.histogram(x, bins=bins)
    total = 1.0*len(x)
    width = 50
    nmax = N.max()
    for (xi, n) in zip(X,N):
        bar = '#'*int(n*1.0*width/nmax)
        xi = '{0: <8.4g}'.format(xi).ljust(10)
        print('{0}| {1}'.format(xi,bar))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment