Create utils/visualize.py
Browse files- utils/visualize.py +28 -0
utils/visualize.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
```
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
import logging
|
5 |
+
|
6 |
+
logger = logging.getLogger(__name__)
|
7 |
+
|
8 |
+
def plot_usage(df):
|
9 |
+
"""Plot daily usage trends from log data."""
|
10 |
+
logger.info("Generating usage plot...")
|
11 |
+
try:
|
12 |
+
df["timestamp"] = pd.to_datetime(df["timestamp"])
|
13 |
+
daily_usage = df.groupby([df["timestamp"].dt.date, "equipment"]).size().unstack(fill_value=0)
|
14 |
+
|
15 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
16 |
+
daily_usage.plot(kind="line", ax=ax)
|
17 |
+
ax.set_title("Daily Usage Trends")
|
18 |
+
ax.set_xlabel("Date")
|
19 |
+
ax.set_ylabel("Usage Count")
|
20 |
+
ax.legend(title="Equipment")
|
21 |
+
plt.tight_layout()
|
22 |
+
|
23 |
+
logger.info("Usage plot generated successfully.")
|
24 |
+
return fig
|
25 |
+
except Exception as e:
|
26 |
+
logger.error(f"Failed to generate usage plot: {e}")
|
27 |
+
raise
|
28 |
+
```
|