From 6973093a2b80b4fb9e216d1b3e329dd4c2badc7c Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 8 Sep 2024 14:13:41 +0200 Subject: Refactor finance.visualize.py This introduces the plot() helper function to plot the default data format list[tuple[datetime, Decimal]] with matplotlib. --- finance/visualize.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'finance/visualize.py') diff --git a/finance/visualize.py b/finance/visualize.py index a13ac22..2c70f59 100644 --- a/finance/visualize.py +++ b/finance/visualize.py @@ -8,19 +8,26 @@ def display( simulated: list[tuple[datetime, Decimal]], measured: list[tuple[datetime, Decimal]], ) -> None: - matplotlib.pyplot.plot( - [i[0] for i in simulated], # type: ignore - [float(i[1]) for i in simulated], - label="Simulated", - ) - matplotlib.pyplot.plot( - [i[0] for i in measured], # type: ignore - [float(i[1]) for i in measured], - label="Measured", - marker="x", - linestyle="None", - ) - matplotlib.pyplot.xlabel("Time") matplotlib.pyplot.ylabel("Money") + matplotlib.pyplot.xlabel("Time") + + plot(data=simulated, label="Simulated") + plot(data=measured, label="Measured", marker=".", linestyle="none") matplotlib.pyplot.legend() + matplotlib.pyplot.show() + + +def plot( + data: list[tuple[datetime, Decimal]], + label: str, + marker: str = "", + linestyle: str = "solid", +) -> None: + matplotlib.pyplot.plot( + [i[0] for i in data], # type: ignore + [float(i[1]) for i in data], + label=label, + marker=marker, + linestyle=linestyle, + ) -- cgit v1.2.3-70-g09d2