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()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.