| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def plot_usage(df): | |
| """Plot daily usage trends from log data.""" | |
| logger.info("Generating usage plot...") | |
| try: | |
| df["timestamp"] = pd.to_datetime(df["timestamp"]) | |
| daily_usage = df.groupby([df["timestamp"].dt.date, "equipment"]).size().unstack(fill_value=0) | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| daily_usage.plot(kind="line", ax=ax) | |
| ax.set_title("Daily Usage Trends") | |
| ax.set_xlabel("Date") | |
| ax.set_ylabel("Usage Count") | |
| ax.legend(title="Equipment") | |
| plt.tight_layout() | |
| logger.info("Usage plot generated successfully.") | |
| return fig | |
| except Exception as e: | |
| logger.error(f"Failed to generate usage plot: {e}") | |
| raise |