Torn.com-ActivityTracker/app/analysis/plot_bar_peak_hours.py
Michael Beck 5e98a0ba47 init
2025-02-22 16:55:41 +01:00

54 lines
1.6 KiB
Python

import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from .basePlotAnalysis import BasePlotAnalysis
import matplotlib
matplotlib.use('Agg')
class PlotPeakHours(BasePlotAnalysis):
"""
Class for analyzing peak activity hours and generating a bar chart.
Attributes:
name (str): The name of the analysis.
description (str): A brief description of the analysis.
plot_filename (str): The filename for the output plot.
note (str): Additional notes for the analysis.
"""
name = "Peak Hours Analysis"
description = "Identifies peak activity hours using a bar chart."
plot_filename = "peak_hours.png"
note = ""
def transform_data(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Transform data to add was_active column and extract peak hours. See data_utils.py.
Parameters:
df (pd.DataFrame): The input DataFrame containing user activity data.
Returns:
pd.DataFrame: The transformed DataFrame with additional columns for analysis.
"""
return df
def plot_data(self, df: pd.DataFrame):
"""
Generate bar chart for peak hours.
Parameters:
df (pd.DataFrame): The transformed DataFrame containing user activity data.
"""
peak_hours = df[df["was_active"]]["hour"].value_counts().sort_index()
plt.figure(figsize=(12, 5))
sns.barplot(x=peak_hours.index, y=peak_hours.values, hue=peak_hours.values, palette="coolwarm")
plt.xlabel("Hour of the Day")
plt.ylabel("Activity Count")
plt.title("Peak Hours of User Activity")
plt.xticks(range(0, 24))