Skip to content
Merged
54 changes: 54 additions & 0 deletions plots/altair/point/scatter-color-groups/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
scatter-color-groups: Scatter Plot with Color Groups
Library: altair
"""

import altair as alt
import numpy as np
import pandas as pd


# Data - create iris-like dataset with three species groups
np.random.seed(42)

# Generate data for three groups with different cluster centers
n_per_group = 50

# Setosa: smaller sepal length, larger sepal width
setosa_x = np.random.normal(5.0, 0.4, n_per_group)
setosa_y = np.random.normal(3.4, 0.4, n_per_group)

# Versicolor: medium values
versicolor_x = np.random.normal(6.0, 0.5, n_per_group)
versicolor_y = np.random.normal(2.8, 0.3, n_per_group)

# Virginica: larger sepal length, medium sepal width
virginica_x = np.random.normal(6.6, 0.6, n_per_group)
virginica_y = np.random.normal(3.0, 0.35, n_per_group)

data = pd.DataFrame(
{
"sepal_length": np.concatenate([setosa_x, versicolor_x, virginica_x]),
"sepal_width": np.concatenate([setosa_y, versicolor_y, virginica_y]),
"species": ["setosa"] * n_per_group + ["versicolor"] * n_per_group + ["virginica"] * n_per_group,
}
)

# Define custom color palette (colorblind-safe)
color_scale = alt.Scale(domain=["setosa", "versicolor", "virginica"], range=["#306998", "#FFD43B", "#059669"])

# Create scatter plot with color groups
chart = (
alt.Chart(data)
.mark_point(size=100, opacity=0.7)
.encode(
x=alt.X("sepal_length:Q", title="Sepal Length (cm)"),
y=alt.Y("sepal_width:Q", title="Sepal Width (cm)"),
color=alt.Color("species:N", title="Species", scale=color_scale),
tooltip=["species:N", "sepal_length:Q", "sepal_width:Q"],
)
.properties(width=1600, height=900, title="Scatter Plot with Color Groups")
)

# Save as PNG (1600 × 900 × 3 = 4800 × 2700 px)
chart.save("plot.png", scale_factor=3.0)
65 changes: 65 additions & 0 deletions plots/bokeh/scatter/scatter-color-groups/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
scatter-color-groups: Scatter Plot with Color Groups
Library: bokeh
"""

import numpy as np
import pandas as pd
from bokeh.io import export_png
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure


# Data - Iris-like dataset
np.random.seed(42)
n_per_group = 50

data = pd.DataFrame({
"sepal_length": np.concatenate([
np.random.normal(5.0, 0.35, n_per_group),
np.random.normal(5.9, 0.50, n_per_group),
np.random.normal(6.6, 0.60, n_per_group),
]),
"sepal_width": np.concatenate([
np.random.normal(3.4, 0.38, n_per_group),
np.random.normal(2.8, 0.30, n_per_group),
np.random.normal(3.0, 0.30, n_per_group),
]),
"species": ["setosa"] * n_per_group + ["versicolor"] * n_per_group + ["virginica"] * n_per_group,
})

# Color palette (from style guide)
colors = ["#306998", "#FFD43B", "#DC2626", "#059669", "#8B5CF6", "#F97316"]
species_list = data["species"].unique().tolist()
color_map = {species: colors[i] for i, species in enumerate(species_list)}
data["color"] = data["species"].map(color_map)

# Create figure
p = figure(
width=4800,
height=2700,
title="Scatter Plot with Color Groups",
x_axis_label="Sepal Length (cm)",
y_axis_label="Sepal Width (cm)",
)

# Plot each group separately for legend
for species in species_list:
species_data = data[data["species"] == species]
source = ColumnDataSource(data={"x": species_data["sepal_length"], "y": species_data["sepal_width"]})
p.scatter(
x="x", y="y", source=source, size=12, alpha=0.7, color=color_map[species], legend_label=species.capitalize()
)

# Styling
p.title.text_font_size = "20pt"
p.xaxis.axis_label_text_font_size = "20pt"
p.yaxis.axis_label_text_font_size = "20pt"
p.xaxis.major_label_text_font_size = "16pt"
p.yaxis.major_label_text_font_size = "16pt"
p.legend.label_text_font_size = "16pt"
p.legend.location = "top_right"
p.grid.grid_line_alpha = 0.3

# Save
export_png(p, filename="plot.png")
166 changes: 166 additions & 0 deletions plots/highcharts/scatter/scatter-color-groups/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"""
scatter-color-groups: Scatter Plot with Color Groups
Library: highcharts

Note: Highcharts requires a license for commercial use.
"""

import json
import tempfile
import time
import urllib.request
from pathlib import Path

from highcharts_core.chart import Chart
from highcharts_core.options import HighchartsOptions
from highcharts_core.options.series.scatter import ScatterSeries
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


# Color palette from style guide
COLORS = ["#306998", "#FFD43B", "#DC2626", "#059669", "#8B5CF6", "#F97316"]

# Data - Iris dataset (sepal_length, sepal_width) by species
# fmt: off
iris_data = {
"setosa": [
(5.1, 3.5), (4.9, 3.0), (4.7, 3.2), (4.6, 3.1), (5.0, 3.6), (5.4, 3.9), (4.6, 3.4), (5.0, 3.4),
(4.4, 2.9), (4.9, 3.1), (5.4, 3.7), (4.8, 3.4), (4.8, 3.0), (4.3, 3.0), (5.8, 4.0), (5.7, 4.4),
(5.4, 3.9), (5.1, 3.5), (5.7, 3.8), (5.1, 3.8), (5.4, 3.4), (5.1, 3.7), (4.6, 3.6), (5.1, 3.3),
(4.8, 3.4), (5.0, 3.0), (5.0, 3.4), (5.2, 3.5), (5.2, 3.4), (4.7, 3.2), (4.8, 3.1), (5.4, 3.4),
(5.2, 4.1), (5.5, 4.2), (4.9, 3.1), (5.0, 3.2), (5.5, 3.5), (4.9, 3.6), (4.4, 3.0), (5.1, 3.4),
(5.0, 3.5), (4.5, 2.3), (4.4, 3.2), (5.0, 3.5), (5.1, 3.8), (4.8, 3.0), (5.1, 3.8), (4.6, 3.2),
(5.3, 3.7), (5.0, 3.3),
],
"versicolor": [
(7.0, 3.2), (6.4, 3.2), (6.9, 3.1), (5.5, 2.3), (6.5, 2.8), (5.7, 2.8), (6.3, 3.3), (4.9, 2.4),
(6.6, 2.9), (5.2, 2.7), (5.0, 2.0), (5.9, 3.0), (6.0, 2.2), (6.1, 2.9), (5.6, 2.9), (6.7, 3.1),
(5.6, 3.0), (5.8, 2.7), (6.2, 2.2), (5.6, 2.5), (5.9, 3.2), (6.1, 2.8), (6.3, 2.5), (6.1, 2.8),
(6.4, 2.9), (6.6, 3.0), (6.8, 2.8), (6.7, 3.0), (6.0, 2.9), (5.7, 2.6), (5.5, 2.4), (5.5, 2.4),
(5.8, 2.7), (6.0, 2.7), (5.4, 3.0), (6.0, 3.4), (6.7, 3.1), (6.3, 2.3), (5.6, 3.0), (5.5, 2.5),
(5.5, 2.6), (6.1, 3.0), (5.8, 2.6), (5.0, 2.3), (5.6, 2.7), (5.7, 3.0), (5.7, 2.9), (6.2, 2.9),
(5.1, 2.5), (5.7, 2.8),
],
"virginica": [
(6.3, 3.3), (5.8, 2.7), (7.1, 3.0), (6.3, 2.9), (6.5, 3.0), (7.6, 3.0), (4.9, 2.5), (7.3, 2.9),
(6.7, 2.5), (7.2, 3.6), (6.5, 3.2), (6.4, 2.7), (6.8, 3.0), (5.7, 2.5), (5.8, 2.8), (6.4, 3.2),
(6.5, 3.0), (7.7, 3.8), (7.7, 2.6), (6.0, 2.2), (6.9, 3.2), (5.6, 2.8), (7.7, 2.8), (6.3, 2.7),
(6.7, 3.3), (7.2, 3.2), (6.2, 2.8), (6.1, 3.0), (6.4, 2.8), (7.2, 3.0), (7.4, 2.8), (7.9, 3.8),
(6.4, 2.8), (6.3, 2.8), (6.1, 2.6), (7.7, 3.0), (6.3, 3.4), (6.4, 3.1), (6.0, 3.0), (6.9, 3.1),
(6.7, 3.1), (6.9, 3.1), (5.8, 2.7), (6.8, 3.2), (6.7, 3.3), (6.7, 3.0), (6.3, 2.5), (6.5, 3.0),
(6.2, 3.4), (5.9, 3.0),
],
}
# fmt: on
groups = list(iris_data.keys())

# Create chart with container ID for rendering
chart = Chart(container="container")
chart.options = HighchartsOptions()

# Chart configuration - 4800 x 2700 px per style guide
chart.options.chart = {"type": "scatter", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"}

# Title
chart.options.title = {
"text": "Iris Dataset: Sepal Dimensions by Species",
"style": {"fontSize": "48px", "fontWeight": "bold"},
}

# X-axis configuration
chart.options.x_axis = {
"title": {"text": "Sepal Length (cm)", "style": {"fontSize": "36px"}},
"labels": {"style": {"fontSize": "28px"}},
"gridLineWidth": 1,
"gridLineDashStyle": "Dot",
"gridLineColor": "rgba(0, 0, 0, 0.15)",
}

# Y-axis configuration
chart.options.y_axis = {
"title": {"text": "Sepal Width (cm)", "style": {"fontSize": "36px"}},
"labels": {"style": {"fontSize": "28px"}},
"gridLineWidth": 1,
"gridLineDashStyle": "Dot",
"gridLineColor": "rgba(0, 0, 0, 0.15)",
}

# Plot options for scatter
chart.options.plot_options = {
"scatter": {
"marker": {"radius": 12, "states": {"hover": {"enabled": True, "lineColor": "rgb(100,100,100)"}}},
"states": {"hover": {"marker": {"enabled": False}}},
}
}

# Add a series for each group with distinct colors
for i, group in enumerate(groups):
series = ScatterSeries()
series.name = group.capitalize()
series.data = iris_data[group]
series.color = COLORS[i % len(COLORS)]
chart.add_series(series)

# Legend configuration
chart.options.legend = {
"enabled": True,
"align": "right",
"verticalAlign": "middle",
"layout": "vertical",
"itemStyle": {"fontSize": "28px"},
}

# Tooltip configuration
chart.options.tooltip = {
"headerFormat": "<b>{series.name}</b><br>",
"pointFormat": "Sepal Length: {point.x} cm<br>Sepal Width: {point.y} cm",
"style": {"fontSize": "24px"},
}

# Disable credits
chart.options.credits = {"enabled": False}

# Export to PNG via Selenium screenshot
# Download Highcharts JS (required for headless Chrome which can't load CDN)
highcharts_url = "https://code.highcharts.com/highcharts.js"
with urllib.request.urlopen(highcharts_url, timeout=30) as response:
highcharts_js = response.read().decode("utf-8")

# Get chart options as JSON
opts_json = json.dumps(chart.options.to_dict())

html_content = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>{highcharts_js}</script>
</head>
<body style="margin:0; padding:0; overflow:hidden;">
<div id="container" style="width: 4800px; height: 2700px;"></div>
<script>
Highcharts.chart('container', {opts_json});
</script>
</body>
</html>"""

# Write temp HTML and take screenshot
with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f:
f.write(html_content)
temp_path = f.name

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=4800,2800")

driver = webdriver.Chrome(options=chrome_options)
driver.get(f"file://{temp_path}")
time.sleep(5) # Wait for chart to render
driver.save_screenshot("plot.png")
driver.quit()

Path(temp_path).unlink() # Clean up temp file
print("Plot saved to plot.png")
70 changes: 70 additions & 0 deletions plots/letsplot/point/scatter-color-groups/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
scatter-color-groups: Scatter Plot with Color Groups
Library: letsplot
"""

import numpy as np
import pandas as pd
from lets_plot import (
LetsPlot,
aes,
element_text,
geom_point,
ggplot,
ggsave,
ggsize,
labs,
scale_color_manual,
theme,
theme_minimal,
)


LetsPlot.setup_html()

# Data - Generate iris-like dataset
np.random.seed(42)
n_per_species = 50

# Setosa: smaller sepals
setosa_length = np.random.normal(5.0, 0.35, n_per_species)
setosa_width = np.random.normal(3.4, 0.38, n_per_species)

# Versicolor: medium sepals
versicolor_length = np.random.normal(5.9, 0.52, n_per_species)
versicolor_width = np.random.normal(2.8, 0.31, n_per_species)

# Virginica: larger sepals
virginica_length = np.random.normal(6.6, 0.64, n_per_species)
virginica_width = np.random.normal(3.0, 0.32, n_per_species)

data = pd.DataFrame(
{
"sepal_length": np.concatenate([setosa_length, versicolor_length, virginica_length]),
"sepal_width": np.concatenate([setosa_width, versicolor_width, virginica_width]),
"species": ["Setosa"] * n_per_species + ["Versicolor"] * n_per_species + ["Virginica"] * n_per_species,
}
)

# Custom color palette (colorblind-safe)
colors = ["#306998", "#FFD43B", "#059669"]

# Plot
plot = (
ggplot(data, aes(x="sepal_length", y="sepal_width", color="species"))
+ geom_point(size=4, alpha=0.7)
+ scale_color_manual(values=colors)
+ labs(x="Sepal Length (cm)", y="Sepal Width (cm)", title="Iris Sepal Dimensions by Species", color="Species")
+ theme_minimal()
+ theme(
plot_title=element_text(size=20),
axis_title=element_text(size=20),
axis_text=element_text(size=16),
legend_title=element_text(size=16),
legend_text=element_text(size=16),
)
+ ggsize(1600, 900)
)

# Save (scale 3x to get 4800 x 2700 px)
ggsave(plot, "plot.png", path=".", scale=3)
Loading
Loading