-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.py
More file actions
30 lines (25 loc) · 768 Bytes
/
canvas.py
File metadata and controls
30 lines (25 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import matplotlib.pyplot as plt
class Canvas:
def __init__(self, width=50, height=50):
self.width = width
self.height = height
self.pixels = []
def plot(self, x, y):
"""Store a pixel"""
self.pixels.append((x, y))
def show(self, title):
"""Display pixels"""
if not self.pixels:
print("No pixels to display!")
return
x_vals, y_vals = zip(*self.pixels)
plt.figure(figsize=(6, 6))
plt.scatter(x_vals, y_vals, s=20)
plt.title(title)
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.gca().set_aspect('equal', adjustable='box')
plt.xlim(0, self.width)
plt.ylim(0, self.height)
plt.show()