HP Forums

Full Version: Numworks: Python Graphics
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Numworks Python Scripts: Basic Graphics

Script: atari.py

Draws the basic-eight color palette of the classic 1977 Atari 2600.

Code:
```from math import * from kandinsky import * # 2020-05-28 atari 2600 colors # kandinsky module fill_rect(0,0,320,240,color(245,245,245)) fill_rect(15,15,55,55,color(0,0,0)) fill_rect(85,15,55,55,color(255,0,0)) fill_rect(155,15,55,55,color(255,255,0)) fill_rect(15,85,55,55,color(255,0,255)) fill_rect(155,85,55,55,color(0,255,0)) fill_rect(15,155,55,55,color(0,255,255)) fill_rect(85,155,55,55,color(0,0,255)) fill_rect(155,155,55,55,color(255,255,255)) draw_string("8",107,107)```

Script: firstdigit.py

The tenths digit from n random numbers is extracted and a bar chart is generated based on the results. I recommend a sample size of at least 20.

Code:
```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()```

Script: colorfulrings.py

The script cycles through a set of nine colors, four times. The Kandinsky module is used to generate the flowery circles as well as cycle through the colors. This module works with integer pixels.

Code:
```from math import * from kandinsky import * from time import * # color lists r=[255,255,255,0,0,0,51,128,255] g=[0,102,255,128,255,0,102,128,255] b=[0,0,0,0,0,255,255,128,255] # angles a=list(range(128)) for i in range(128):   a[i]=i/128*2*pi # draw circles for k in range(36):   n=int(fmod(k,9))   for j in range(50):     for i in range(128):       x=int(160+(20+j)*cos(a[i]))       y=int(120+(20+j)*sin(a[i]))       set_pixel(x,y,color(r[n],g[n],b[n]))   sleep(0.1)```

Script: modulusplot.py

Generate a pixel plot of the equation (x^n + y^n) mod m

Code:
```from math import * from kandinsky import * print("EWS 2020-05-28") print("x**n + y**n mod m") n=float(input("power? ")) m=float(input("modulus? ")) for x in range(320):   for y in range(240):     t=fmod(pow(x,n)+pow(y,n),m)       c=floor(t/m*255)     set_pixel(x+1,y+1,color(c,c,c))```