|
| 1 | +""" |
| 2 | +scatter-color-groups: Scatter Plot with Color Groups |
| 3 | +Library: highcharts |
| 4 | +
|
| 5 | +Note: Highcharts requires a license for commercial use. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import tempfile |
| 10 | +import time |
| 11 | +import urllib.request |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from highcharts_core.chart import Chart |
| 15 | +from highcharts_core.options import HighchartsOptions |
| 16 | +from highcharts_core.options.series.scatter import ScatterSeries |
| 17 | +from selenium import webdriver |
| 18 | +from selenium.webdriver.chrome.options import Options |
| 19 | + |
| 20 | + |
| 21 | +# Color palette from style guide |
| 22 | +COLORS = ["#306998", "#FFD43B", "#DC2626", "#059669", "#8B5CF6", "#F97316"] |
| 23 | + |
| 24 | +# Data - Iris dataset (sepal_length, sepal_width) by species |
| 25 | +# fmt: off |
| 26 | +iris_data = { |
| 27 | + "setosa": [ |
| 28 | + (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), |
| 29 | + (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), |
| 30 | + (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), |
| 31 | + (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), |
| 32 | + (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), |
| 33 | + (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), |
| 34 | + (5.3, 3.7), (5.0, 3.3), |
| 35 | + ], |
| 36 | + "versicolor": [ |
| 37 | + (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), |
| 38 | + (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), |
| 39 | + (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), |
| 40 | + (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), |
| 41 | + (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), |
| 42 | + (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), |
| 43 | + (5.1, 2.5), (5.7, 2.8), |
| 44 | + ], |
| 45 | + "virginica": [ |
| 46 | + (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), |
| 47 | + (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), |
| 48 | + (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), |
| 49 | + (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), |
| 50 | + (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), |
| 51 | + (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), |
| 52 | + (6.2, 3.4), (5.9, 3.0), |
| 53 | + ], |
| 54 | +} |
| 55 | +# fmt: on |
| 56 | +groups = list(iris_data.keys()) |
| 57 | + |
| 58 | +# Create chart with container ID for rendering |
| 59 | +chart = Chart(container="container") |
| 60 | +chart.options = HighchartsOptions() |
| 61 | + |
| 62 | +# Chart configuration - 4800 x 2700 px per style guide |
| 63 | +chart.options.chart = {"type": "scatter", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"} |
| 64 | + |
| 65 | +# Title |
| 66 | +chart.options.title = { |
| 67 | + "text": "Iris Dataset: Sepal Dimensions by Species", |
| 68 | + "style": {"fontSize": "48px", "fontWeight": "bold"}, |
| 69 | +} |
| 70 | + |
| 71 | +# X-axis configuration |
| 72 | +chart.options.x_axis = { |
| 73 | + "title": {"text": "Sepal Length (cm)", "style": {"fontSize": "36px"}}, |
| 74 | + "labels": {"style": {"fontSize": "28px"}}, |
| 75 | + "gridLineWidth": 1, |
| 76 | + "gridLineDashStyle": "Dot", |
| 77 | + "gridLineColor": "rgba(0, 0, 0, 0.15)", |
| 78 | +} |
| 79 | + |
| 80 | +# Y-axis configuration |
| 81 | +chart.options.y_axis = { |
| 82 | + "title": {"text": "Sepal Width (cm)", "style": {"fontSize": "36px"}}, |
| 83 | + "labels": {"style": {"fontSize": "28px"}}, |
| 84 | + "gridLineWidth": 1, |
| 85 | + "gridLineDashStyle": "Dot", |
| 86 | + "gridLineColor": "rgba(0, 0, 0, 0.15)", |
| 87 | +} |
| 88 | + |
| 89 | +# Plot options for scatter |
| 90 | +chart.options.plot_options = { |
| 91 | + "scatter": { |
| 92 | + "marker": {"radius": 12, "states": {"hover": {"enabled": True, "lineColor": "rgb(100,100,100)"}}}, |
| 93 | + "states": {"hover": {"marker": {"enabled": False}}}, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +# Add a series for each group with distinct colors |
| 98 | +for i, group in enumerate(groups): |
| 99 | + series = ScatterSeries() |
| 100 | + series.name = group.capitalize() |
| 101 | + series.data = iris_data[group] |
| 102 | + series.color = COLORS[i % len(COLORS)] |
| 103 | + chart.add_series(series) |
| 104 | + |
| 105 | +# Legend configuration |
| 106 | +chart.options.legend = { |
| 107 | + "enabled": True, |
| 108 | + "align": "right", |
| 109 | + "verticalAlign": "middle", |
| 110 | + "layout": "vertical", |
| 111 | + "itemStyle": {"fontSize": "28px"}, |
| 112 | +} |
| 113 | + |
| 114 | +# Tooltip configuration |
| 115 | +chart.options.tooltip = { |
| 116 | + "headerFormat": "<b>{series.name}</b><br>", |
| 117 | + "pointFormat": "Sepal Length: {point.x} cm<br>Sepal Width: {point.y} cm", |
| 118 | + "style": {"fontSize": "24px"}, |
| 119 | +} |
| 120 | + |
| 121 | +# Disable credits |
| 122 | +chart.options.credits = {"enabled": False} |
| 123 | + |
| 124 | +# Export to PNG via Selenium screenshot |
| 125 | +# Download Highcharts JS (required for headless Chrome which can't load CDN) |
| 126 | +highcharts_url = "https://code.highcharts.com/highcharts.js" |
| 127 | +with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 128 | + highcharts_js = response.read().decode("utf-8") |
| 129 | + |
| 130 | +# Get chart options as JSON |
| 131 | +opts_json = json.dumps(chart.options.to_dict()) |
| 132 | + |
| 133 | +html_content = f"""<!DOCTYPE html> |
| 134 | +<html> |
| 135 | +<head> |
| 136 | + <meta charset="utf-8"> |
| 137 | + <script>{highcharts_js}</script> |
| 138 | +</head> |
| 139 | +<body style="margin:0; padding:0; overflow:hidden;"> |
| 140 | + <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 141 | + <script> |
| 142 | + Highcharts.chart('container', {opts_json}); |
| 143 | + </script> |
| 144 | +</body> |
| 145 | +</html>""" |
| 146 | + |
| 147 | +# Write temp HTML and take screenshot |
| 148 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
| 149 | + f.write(html_content) |
| 150 | + temp_path = f.name |
| 151 | + |
| 152 | +chrome_options = Options() |
| 153 | +chrome_options.add_argument("--headless") |
| 154 | +chrome_options.add_argument("--no-sandbox") |
| 155 | +chrome_options.add_argument("--disable-dev-shm-usage") |
| 156 | +chrome_options.add_argument("--disable-gpu") |
| 157 | +chrome_options.add_argument("--window-size=4800,2800") |
| 158 | + |
| 159 | +driver = webdriver.Chrome(options=chrome_options) |
| 160 | +driver.get(f"file://{temp_path}") |
| 161 | +time.sleep(5) # Wait for chart to render |
| 162 | +driver.save_screenshot("plot.png") |
| 163 | +driver.quit() |
| 164 | + |
| 165 | +Path(temp_path).unlink() # Clean up temp file |
| 166 | +print("Plot saved to plot.png") |
0 commit comments