Skip to content

Instantly share code, notes, and snippets.

@tammoippen
Last active March 20, 2024 17:54
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tammoippen/4474e838e969bf177155231ebba52386 to your computer and use it in GitHub Desktop.
Save tammoippen/4474e838e969bf177155231ebba52386 to your computer and use it in GitHub Desktop.
Draw a crappy text-mode histogram of an array (python 3)
import numpy as np
def crappyhist(a, bins=50, width=140):
h, b = np.histogram(a, bins)
for i in range (0, bins):
print('{:12.5f} | {:{width}s} {}'.format(
b[i],
'#'*int(width*h[i]/np.amax(h)),
h[i],
width=width))
print('{:12.5f} |'.format(b[bins]))
@tammoippen
Copy link
Author

tammoippen commented Jun 19, 2017

Modified from: http://kevinastraight.x10host.com/2013/12/28/python-histograms-from-the-console/

Output something like:

>> crappyhist(np.random.normal(size=500))
    -2.83445  | ##########                                                                                                                                   2
    -2.72718  |                                                                                                                                              0
    -2.61991  |                                                                                                                                              0
    -2.51264  | #####                                                                                                                                        1
    -2.40536  | ###############                                                                                                                              3
    -2.29809  | ##########                                                                                                                                   2
    -2.19082  | ###############                                                                                                                              3
    -2.08355  | #####                                                                                                                                        1
    -1.97627  | ####################                                                                                                                         4
    -1.86900  | ###############################                                                                                                              6
    -1.76173  | ###############                                                                                                                              3
    -1.65446  | ###############################                                                                                                              6
    -1.54718  | #########################################                                                                                                    8
    -1.43991  | ###################################################                                                                                          10
    -1.33264  | #########################################################                                                                                    11
    -1.22537  | ##################################################################################################################                           22
    -1.11809  | #########################                                                                                                                    5
    -1.01082  | ########################################################################                                                                     14
    -0.90355  | ##################################################################################                                                           16
    -0.79627  | #######################################################################################################                                      20
    -0.68900  | ########################################################################################                                                     17
    -0.58173  | ##################################################################################################                                           19
    -0.47446  | ############################################################################################################                                 21
    -0.36718  | #############################################################################################                                                18
    -0.25991  | ##################################################################################################                                           19
    -0.15264  | #############################################################################################                                                18
    -0.04537  | ############################################################################################################################                 24
     0.06191  | #################################################################################################################################            25
     0.16918  | ########################################################################                                                                     14
     0.27645  | #############################################################################################                                                18
     0.38372  | ############################################################################################################                                 21
     0.49100  | ###################################################                                                                                          10
     0.59827  | ############################################################################################################################################ 27
     0.70554  | #######################################################################################################                                      20
     0.81281  | ########################################################################                                                                     14
     0.92009  | ##################################################################################                                                           16
     1.02736  | ##############################################                                                                                               9
     1.13463  | #########################################                                                                                                    8
     1.24190  | ####################################                                                                                                         7
     1.34918  | #########################################                                                                                                    8
     1.45645  | #########################                                                                                                                    5
     1.56372  | #########################                                                                                                                    5
     1.67099  | ##########                                                                                                                                   2
     1.77827  | ##########                                                                                                                                   2
     1.88554  | #########################                                                                                                                    5
     1.99281  | #########################                                                                                                                    5
     2.10008  | #####                                                                                                                                        1
     2.20736  | #####                                                                                                                                        1
     2.31463  | #####                                                                                                                                        1
     2.42190  | ###############                                                                                                                              3
     2.52917  |

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