Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions plots/facet-grid/implementations/plotly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
""" pyplots.ai
facet-grid: Faceted Grid Plot
Library: plotly 6.5.0 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-30
"""

import numpy as np
import pandas as pd
import plotly.express as px


# Data - Create realistic dataset for faceted visualization
np.random.seed(42)

# Product performance data across regions and quarters
regions = ["North", "South", "East", "West"]
quarters = ["Q1", "Q2", "Q3", "Q4"]

data = []
for region in regions:
for quarter in quarters:
# Generate 15 data points per region-quarter combination
n = 15
# Base values vary by region and quarter
base_sales = {"North": 80, "South": 60, "East": 70, "West": 75}
quarter_effect = {"Q1": 0, "Q2": 10, "Q3": 5, "Q4": 15}

marketing_spend = np.random.uniform(10, 50, n)
sales = (
base_sales[region]
+ quarter_effect[quarter]
+ marketing_spend * (0.8 + np.random.uniform(0, 0.4, n))
+ np.random.normal(0, 5, n)
)

for i in range(n):
data.append(
{
"Marketing Spend ($K)": marketing_spend[i],
"Sales ($K)": sales[i],
"Region": region,
"Quarter": quarter,
}
)

df = pd.DataFrame(data)

# Create faceted scatter plot
# Using single color since Region is already encoded by facet rows
fig = px.scatter(df, x="Marketing Spend ($K)", y="Sales ($K)", facet_row="Region", facet_col="Quarter")

# Update layout for large canvas
fig.update_layout(
title=dict(text="facet-grid · plotly · pyplots.ai", font=dict(size=32), x=0.5, xanchor="center"),
font=dict(size=16),
template="plotly_white",
showlegend=False,
margin=dict(l=80, r=80, t=100, b=80),
)

# Update axes for all facets
fig.update_xaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1)

fig.update_yaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1)

# Update facet labels (annotations)
fig.for_each_annotation(lambda a: a.update(font=dict(size=18)))

# Update markers with consistent color for visibility
fig.update_traces(marker=dict(size=12, opacity=0.8, color="#306998", line=dict(width=1, color="white")))

# Save as PNG and HTML
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html", include_plotlyjs="cdn")
29 changes: 29 additions & 0 deletions plots/facet-grid/metadata/plotly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
library: plotly
specification_id: facet-grid
created: '2025-12-30T16:30:15Z'
updated: '2025-12-30T16:47:28Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20601057130
issue: 0
python_version: 3.13.11
library_version: 6.5.0
preview_url: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot_thumb.png
preview_html: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot.html
quality_score: 91
review:
strengths:
- Clean implementation using Plotly Express native faceting capability (facet_row,
facet_col)
- Excellent data generation with realistic business scenario (marketing spend vs
sales by region and quarter)
- Good visual hierarchy with clear title, readable axis labels with units, and subtle
grid
- Produces both PNG and HTML output leveraging Plotly interactivity
- Proper use of template=plotly_white for clean appearance
weaknesses:
- Legend showing regions is redundant since region is already encoded by facet rows
- Markers could be slightly larger (15-18 instead of 12) for better visibility with
~15 points per facet
- Row facet labels are displayed vertically which reduces readability compared to
horizontal text