-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_series.py
51 lines (36 loc) · 1.21 KB
/
graph_series.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
from typing import Callable
import generator
import series
number: int = 0
TERMS: list[int] = []
ESTIMATES: list[float] = []
ERRORS: list[float] = []
SERIES: Callable[[int], series.GeneratorType] = series.series1()
def plot(n: int) -> None:
global number
for ax in (ax1, ax2):
ax.cla()
ax.plot(0, 0, color="black", linewidth=3, marker="o")
ax.axhline(y=0, color="black", linewidth=3)
ax.axvline(x=0, color="black", linewidth=3)
ax1.axhline(
y=series.math.pi, color="red", linewidth=2,
linestyle="--", label="y = π"
)
ax1.plot(TERMS, ESTIMATES)
ax2.plot(TERMS, ERRORS)
ax1.set_ylabel("Value of Pi")
ax2.set_ylabel("Percentage Error in Calculation")
ax2.set_xlabel("Number of Terms")
ax1.grid(True)
ax2.grid(True)
ax1.legend(loc="lower right")
TERMS.append(number := number + 1)
ESTIMATES.append(pi := next(SERIES))
ERRORS.append(generator.error(pi))
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
animate = FuncAnimation(fig, plot, interval=1)
if __name__ == "__main__":
plt.show()