firstdigit.py

Created by ews31415

Created on May 29, 2020

578 Bytes

Generate a bar chart of n integers (recommended that n be at least 20). The first digit from


from math import *
from random import *
from matplotlib.pyplot import *

# set up lists
x=[0,1,2,3,4,5,6,7,8,9]
y=[0,0,0,0,0,0,0,0,0,0]

# user iput
print("EWS 2020-05-29")
print("Bar Chart: First Digit")
print("Recommended at least 20")
n=int(input("n? "))

# generate list
for i in range(n):
  s=int(random()*10)
  y[s]=y[s]+1

# bar plot
h=int(n/2)
d=-int(h/4)
axis([-0.5,9.5,d,h])
bar(x,y)

# turn axis off
axis("off")

# labels at the bottom
# results at top
m=max(y)
for i in range(10):
  text(i-0.25,d+1,str(i))
  text(i-0.25,m+2,str(y[i]))
  
show()