|
| 1 | +""" pyplots.ai |
| 2 | +scatter-annotated: Annotated Scatter Plot with Text Labels |
| 3 | +Library: matplotlib 3.10.8 | Python 3.13.11 |
| 4 | +Quality: 92/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +# Data - Technology companies with market cap and revenue |
| 12 | +# Points intentionally spread out to minimize annotation overlap |
| 13 | +np.random.seed(42) |
| 14 | +companies = [ |
| 15 | + "TechCorp", |
| 16 | + "DataSys", |
| 17 | + "CloudNet", |
| 18 | + "ByteWorks", |
| 19 | + "QuantumAI", |
| 20 | + "NexGen", |
| 21 | + "CyberFlow", |
| 22 | + "InfoPrime", |
| 23 | + "CodeLabs", |
| 24 | + "DigiCore", |
| 25 | + "NetSphere", |
| 26 | + "AlgoTech", |
| 27 | + "VisionX", |
| 28 | + "PulseTech", |
| 29 | + "ZetaLogic", |
| 30 | +] |
| 31 | +# Market cap in billions (x-axis) |
| 32 | +market_cap = np.array([55, 135, 105, 18, 225, 70, 120, 45, 165, 85, 30, 195, 150, 175, 60]) |
| 33 | +# Annual revenue in billions (y-axis) |
| 34 | +revenue = np.array([12, 42, 30, 4, 65, 18, 38, 10, 52, 24, 6, 58, 45, 50, 15]) |
| 35 | + |
| 36 | +# Create figure (16:9 landscape) |
| 37 | +fig, ax = plt.subplots(figsize=(16, 9)) |
| 38 | + |
| 39 | +# Scatter plot with Python Blue color |
| 40 | +ax.scatter(market_cap, revenue, s=250, alpha=0.7, color="#306998", edgecolors="white", linewidths=2, zorder=3) |
| 41 | + |
| 42 | +# Custom annotation offsets for each point to avoid overlap |
| 43 | +# Positive x_offset = right, negative = left |
| 44 | +# Positive y_offset = up, negative = down |
| 45 | +offsets = [ |
| 46 | + (10, -10), # TechCorp - below right |
| 47 | + (-10, 8), # DataSys - above left |
| 48 | + (10, 8), # CloudNet - above right |
| 49 | + (-10, -12), # ByteWorks - below left |
| 50 | + (10, 8), # QuantumAI - above right |
| 51 | + (-10, 8), # NexGen - above left |
| 52 | + (10, -10), # CyberFlow - below right |
| 53 | + (10, -10), # InfoPrime - below right (adjusted) |
| 54 | + (-12, 8), # CodeLabs - above left (adjusted) |
| 55 | + (10, 8), # DigiCore - above right |
| 56 | + (-10, -10), # NetSphere - below left (adjusted) |
| 57 | + (10, -12), # AlgoTech - below right |
| 58 | + (10, 8), # VisionX - above right (adjusted) |
| 59 | + (10, 10), # PulseTech - above right (adjusted) |
| 60 | + (-10, -10), # ZetaLogic - below left (adjusted) |
| 61 | +] |
| 62 | + |
| 63 | +# Annotate each point with company name |
| 64 | +for i, company in enumerate(companies): |
| 65 | + x_offset, y_offset = offsets[i] |
| 66 | + ha = "left" if x_offset > 0 else "right" |
| 67 | + |
| 68 | + ax.annotate( |
| 69 | + company, |
| 70 | + xy=(market_cap[i], revenue[i]), |
| 71 | + xytext=(x_offset, y_offset), |
| 72 | + textcoords="offset points", |
| 73 | + fontsize=13, |
| 74 | + color="#333333", |
| 75 | + fontweight="medium", |
| 76 | + ha=ha, |
| 77 | + va="center", |
| 78 | + arrowprops={"arrowstyle": "-", "color": "#888888", "lw": 1, "connectionstyle": "arc3,rad=0"}, |
| 79 | + zorder=4, |
| 80 | + ) |
| 81 | + |
| 82 | +# Styling |
| 83 | +ax.set_xlabel("Market Capitalization ($ Billions)", fontsize=20) |
| 84 | +ax.set_ylabel("Annual Revenue ($ Billions)", fontsize=20) |
| 85 | +ax.set_title("scatter-annotated · matplotlib · pyplots.ai", fontsize=24) |
| 86 | +ax.tick_params(axis="both", labelsize=16) |
| 87 | +ax.grid(True, alpha=0.3, linestyle="--", zorder=1) |
| 88 | + |
| 89 | +# Set axis limits with padding |
| 90 | +ax.set_xlim(0, 240) |
| 91 | +ax.set_ylim(0, 70) |
| 92 | + |
| 93 | +plt.tight_layout() |
| 94 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments