|
| 1 | +""" pyplots.ai |
| 2 | +scatter-annotated: Annotated Scatter Plot with Text Labels |
| 3 | +Library: pygal 3.1.0 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pygal |
| 9 | +from pygal.style import Style |
| 10 | + |
| 11 | + |
| 12 | +# Data - Company market performance (market cap vs revenue) |
| 13 | +np.random.seed(42) |
| 14 | +companies = [ |
| 15 | + "TechFlow", |
| 16 | + "DataPrime", |
| 17 | + "CloudNine", |
| 18 | + "NetWave", |
| 19 | + "CodeSphere", |
| 20 | + "ByteLogic", |
| 21 | + "SoftEdge", |
| 22 | + "DevStack", |
| 23 | + "AppForge", |
| 24 | + "WebCore", |
| 25 | + "CyberLink", |
| 26 | + "DigiTech", |
| 27 | +] |
| 28 | + |
| 29 | +# Generate realistic market cap (x) and revenue (y) data in billions |
| 30 | +# Spread out to avoid overlap |
| 31 | +market_cap = np.array([15, 45, 75, 105, 135, 25, 55, 85, 115, 145, 35, 95]) |
| 32 | +revenue = np.array([8, 22, 35, 28, 48, 12, 18, 42, 32, 55, 15, 38]) |
| 33 | + |
| 34 | +# Create color palette - cycle through distinct colors for each company |
| 35 | +colors = ( |
| 36 | + "#306998", # Python Blue |
| 37 | + "#FFD43B", # Python Yellow |
| 38 | + "#E74C3C", # Red |
| 39 | + "#2ECC71", # Green |
| 40 | + "#9B59B6", # Purple |
| 41 | + "#3498DB", # Light Blue |
| 42 | + "#E67E22", # Orange |
| 43 | + "#1ABC9C", # Teal |
| 44 | + "#34495E", # Dark Gray |
| 45 | + "#F39C12", # Gold |
| 46 | + "#16A085", # Dark Teal |
| 47 | + "#8E44AD", # Dark Purple |
| 48 | +) |
| 49 | + |
| 50 | +# Custom style for large canvas with larger value font for annotations |
| 51 | +custom_style = Style( |
| 52 | + background="white", |
| 53 | + plot_background="white", |
| 54 | + foreground="#333333", |
| 55 | + foreground_strong="#333333", |
| 56 | + foreground_subtle="#666666", |
| 57 | + colors=colors, |
| 58 | + title_font_size=48, |
| 59 | + label_font_size=32, |
| 60 | + major_label_font_size=28, |
| 61 | + legend_font_size=24, |
| 62 | + value_font_size=28, # Font size for annotations |
| 63 | + tooltip_font_size=24, |
| 64 | + stroke_width=2, |
| 65 | +) |
| 66 | + |
| 67 | +# Store company names for value formatter |
| 68 | +company_data = {} |
| 69 | + |
| 70 | +# Create XY chart (scatter plot) |
| 71 | +chart = pygal.XY( |
| 72 | + width=4800, |
| 73 | + height=2700, |
| 74 | + style=custom_style, |
| 75 | + title="scatter-annotated · pygal · pyplots.ai", |
| 76 | + x_title="Market Cap (Billion $)", |
| 77 | + y_title="Annual Revenue (Billion $)", |
| 78 | + show_legend=True, |
| 79 | + legend_at_bottom=True, |
| 80 | + legend_at_bottom_columns=6, |
| 81 | + show_x_guides=True, |
| 82 | + show_y_guides=True, |
| 83 | + dots_size=18, |
| 84 | + stroke=False, |
| 85 | + show_dots=True, |
| 86 | + truncate_label=-1, |
| 87 | + x_label_rotation=0, |
| 88 | + range=(0, 65), |
| 89 | + xrange=(0, 165), |
| 90 | + print_values=True, |
| 91 | + print_values_position="top", |
| 92 | +) |
| 93 | + |
| 94 | +# Add each company as individual series with its own color for legend identification |
| 95 | +# Use formatter dict to show company name instead of coordinates |
| 96 | +for i, company in enumerate(companies): |
| 97 | + company_data[(market_cap[i], revenue[i])] = company |
| 98 | + chart.add( |
| 99 | + company, |
| 100 | + [{"value": (market_cap[i], revenue[i]), "label": company, "formatter": lambda x, c=company: c}], |
| 101 | + dots_size=20, |
| 102 | + formatter=lambda x, c=company: c, |
| 103 | + ) |
| 104 | + |
| 105 | +# Render to PNG and HTML |
| 106 | +chart.render_to_png("plot.png") |
| 107 | +chart.render_to_file("plot.html") |
0 commit comments