|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | facet-grid: Faceted Grid Plot |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-30 |
| 3 | +Library: plotly 6.7.0 | Python 3.13.13 |
| 4 | +Quality: 92/100 | Updated: 2026-05-13 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import numpy as np |
8 | 10 | import pandas as pd |
9 | 11 | import plotly.express as px |
10 | 12 |
|
11 | 13 |
|
12 | | -# Data - Create realistic dataset for faceted visualization |
| 14 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 15 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 16 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 17 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 18 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 19 | +GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 20 | +BRAND = "#009E73" |
| 21 | + |
| 22 | +# Data |
13 | 23 | np.random.seed(42) |
14 | 24 |
|
15 | | -# Product performance data across regions and quarters |
16 | 25 | regions = ["North", "South", "East", "West"] |
17 | 26 | quarters = ["Q1", "Q2", "Q3", "Q4"] |
18 | 27 |
|
19 | 28 | data = [] |
20 | 29 | for region in regions: |
21 | 30 | for quarter in quarters: |
22 | | - # Generate 15 data points per region-quarter combination |
23 | 31 | n = 15 |
24 | | - # Base values vary by region and quarter |
25 | 32 | base_sales = {"North": 80, "South": 60, "East": 70, "West": 75} |
26 | 33 | quarter_effect = {"Q1": 0, "Q2": 10, "Q3": 5, "Q4": 15} |
27 | 34 |
|
|
45 | 52 |
|
46 | 53 | df = pd.DataFrame(data) |
47 | 54 |
|
48 | | -# Create faceted scatter plot |
49 | | -# Using single color since Region is already encoded by facet rows |
| 55 | +# Plot |
50 | 56 | fig = px.scatter(df, x="Marketing Spend ($K)", y="Sales ($K)", facet_row="Region", facet_col="Quarter") |
51 | 57 |
|
52 | | -# Update layout for large canvas |
| 58 | +# Style |
53 | 59 | fig.update_layout( |
54 | | - title=dict(text="facet-grid · plotly · pyplots.ai", font=dict(size=32), x=0.5, xanchor="center"), |
55 | | - font=dict(size=16), |
56 | | - template="plotly_white", |
| 60 | + title={"text": "facet-grid · plotly · anyplot.ai", "font": {"size": 28, "color": INK}, "x": 0.5, "xanchor": "center"}, |
| 61 | + paper_bgcolor=PAGE_BG, |
| 62 | + plot_bgcolor=PAGE_BG, |
| 63 | + font={"color": INK}, |
57 | 64 | showlegend=False, |
58 | | - margin=dict(l=80, r=80, t=100, b=80), |
| 65 | + margin={"l": 80, "r": 80, "t": 100, "b": 80}, |
59 | 66 | ) |
60 | 67 |
|
61 | | -# Update axes for all facets |
62 | | -fig.update_xaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1) |
| 68 | +fig.update_xaxes( |
| 69 | + title_font={"size": 22, "color": INK}, tickfont={"size": 18, "color": INK_SOFT}, gridcolor=GRID, linecolor=INK_SOFT |
| 70 | +) |
63 | 71 |
|
64 | | -fig.update_yaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1) |
| 72 | +fig.update_yaxes( |
| 73 | + title_font={"size": 22, "color": INK}, tickfont={"size": 18, "color": INK_SOFT}, gridcolor=GRID, linecolor=INK_SOFT |
| 74 | +) |
65 | 75 |
|
66 | | -# Update facet labels (annotations) |
67 | | -fig.for_each_annotation(lambda a: a.update(font=dict(size=18))) |
| 76 | +fig.for_each_annotation(lambda a: a.update(font={"size": 18, "color": INK})) |
68 | 77 |
|
69 | | -# Update markers with consistent color for visibility |
70 | | -fig.update_traces(marker=dict(size=12, opacity=0.8, color="#306998", line=dict(width=1, color="white"))) |
| 78 | +fig.update_traces(marker={"size": 16, "opacity": 0.8, "color": BRAND, "line": {"width": 1, "color": PAGE_BG}}) |
71 | 79 |
|
72 | | -# Save as PNG and HTML |
73 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
74 | | -fig.write_html("plot.html", include_plotlyjs="cdn") |
| 80 | +# Save |
| 81 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 82 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments