lovelace-plotly-graph-card icon indicating copy to clipboard operation
lovelace-plotly-graph-card copied to clipboard

Implementing a Boxplot

Open smeingast opened this issue 9 months ago • 3 comments

Hello everyone, I am looking to replicate a boxplot visualization using the Plotly Graph Card in Home Assistant, similar to what I've achieved using Matplotlib and Seaborn. Below is the code I used to generate the plot:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

# Set random seed for reproducibility
np.random.seed(42)

# Define days of the week
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

# Create varied capacity data for each day
capacity_means = {
    'Monday': 45,
    'Tuesday': 50,
    'Wednesday': 55,
    'Thursday': 60,
    'Friday': 65,
    'Saturday': 70,
    'Sunday': 75
}

# Generate data
data = []
for day in days:
    # Create a normal distribution for each day with different means
    day_data = np.random.normal(loc=capacity_means[day], scale=10, size=150)  # 150 samples per day
    data.extend(zip([day]*150, day_data))

# Create a DataFrame
df = pd.DataFrame(data, columns=['Day', 'Capacity'])

# Plotting
plt.figure(figsize=(10, 6))
sns.boxplot(x='Day', y='Capacity', data=df, order=days, showmeans=False,
            meanprops={"marker": "o", "markerfacecolor": "red", "markeredgecolor": "black", "markersize": "10"})

# Add labels and title
plt.title('Capacity Distribution by Day of the Week')
plt.xlabel('Day of the Week')
plt.ylabel('Capacity')
plt.legend()

# Show plot
plt.tight_layout()
plt.show()

Objective: I want to display a similar boxplot in Home Assistant using the Plotly Graph Card. The plot should show:

  • Each day of the week on the x-axis.
  • Boxplots for each day with the capability to show mean/median and the 95th percentile.

Would this work with plotly graph card somehow?

Image

smeingast avatar Mar 02 '25 15:03 smeingast

There is type: box in plotly

dbuezas avatar Mar 02 '25 16:03 dbuezas

There is type: box in plotly

Thanks!! I guess interested in understanding how to group data points effectively for visualization. Specifically, if I have a time series spanning several months, is it possible to create a box plot that groups all data points by each weekday? Additionally, could I group all data points for each of the 24 hours in a day?

smeingast avatar Mar 02 '25 18:03 smeingast

This example shows hot to group per week day

dbuezas avatar Mar 02 '25 19:03 dbuezas