|
| 1 | +""" pyplots.ai |
| 2 | +venn-basic: Venn Diagram |
| 3 | +Library: bokeh 3.8.1 | Python 3.13.11 |
| 4 | +Quality: 90/100 | Created: 2025-12-29 |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from bokeh.io import export_png |
| 9 | +from bokeh.models import HoverTool, Label |
| 10 | +from bokeh.plotting import figure, output_file, save |
| 11 | + |
| 12 | + |
| 13 | +# Data - Three sets representing product feature categories |
| 14 | +set_labels = ["Product A", "Product B", "Product C"] |
| 15 | +set_sizes = [100, 80, 60] # Total sizes |
| 16 | +# Overlaps: AB=30, AC=20, BC=25, ABC=10 |
| 17 | +only_a = 100 - 30 - 20 + 10 # 60 |
| 18 | +only_b = 80 - 30 - 25 + 10 # 35 |
| 19 | +only_c = 60 - 20 - 25 + 10 # 25 |
| 20 | +only_ab = 30 - 10 # 20 |
| 21 | +only_ac = 20 - 10 # 10 |
| 22 | +only_bc = 25 - 10 # 15 |
| 23 | +abc = 10 |
| 24 | + |
| 25 | +# Circle parameters for 3-set Venn diagram |
| 26 | +radius = 0.35 |
| 27 | +centers = [ |
| 28 | + (-0.2, 0.15), # A - top left |
| 29 | + (0.2, 0.15), # B - top right |
| 30 | + (0.0, -0.2), # C - bottom center |
| 31 | +] |
| 32 | + |
| 33 | +# Create figure with proper sizing and hover tool |
| 34 | +p = figure( |
| 35 | + width=4800, |
| 36 | + height=2700, |
| 37 | + title="venn-basic · bokeh · pyplots.ai", |
| 38 | + x_range=(-1, 1), |
| 39 | + y_range=(-0.8, 0.8), |
| 40 | + tools="hover", |
| 41 | + toolbar_location=None, |
| 42 | +) |
| 43 | + |
| 44 | +# Colors with transparency |
| 45 | +colors = ["#306998", "#FFD43B", "#4DAF4A"] # Python Blue, Python Yellow, Green |
| 46 | +alpha = 0.4 |
| 47 | + |
| 48 | +# Generate circle points inline (KISS - no helper function) |
| 49 | +n_points = 100 |
| 50 | +theta = np.linspace(0, 2 * np.pi, n_points) |
| 51 | + |
| 52 | +# Draw the three circles as patches with hover tooltips |
| 53 | +for i, (cx, cy) in enumerate(centers): |
| 54 | + x_pts = (cx + radius * np.cos(theta)).tolist() |
| 55 | + y_pts = (cy + radius * np.sin(theta)).tolist() |
| 56 | + p.patch( |
| 57 | + x_pts, |
| 58 | + y_pts, |
| 59 | + fill_color=colors[i], |
| 60 | + fill_alpha=alpha, |
| 61 | + line_color=colors[i], |
| 62 | + line_width=4, |
| 63 | + line_alpha=0.8, |
| 64 | + name=f"set_{i}", |
| 65 | + ) |
| 66 | + |
| 67 | +# Configure hover tool for interactivity |
| 68 | +hover = p.select({"type": HoverTool}) |
| 69 | +hover.tooltips = [("Set", "@name"), ("Region", "Hover over numbers for details")] |
| 70 | +hover.mode = "mouse" |
| 71 | + |
| 72 | +# Add set labels with total sizes outside circles |
| 73 | +label_positions = [ |
| 74 | + (-0.55, 0.50, set_labels[0], f"n={set_sizes[0]}"), # A - top left |
| 75 | + (0.55, 0.50, set_labels[1], f"n={set_sizes[1]}"), # B - top right |
| 76 | + (0.0, -0.62, set_labels[2], f"n={set_sizes[2]}"), # C - bottom |
| 77 | +] |
| 78 | + |
| 79 | +for lx, ly, text, size_text in label_positions: |
| 80 | + label = Label( |
| 81 | + x=lx, |
| 82 | + y=ly, |
| 83 | + text=text, |
| 84 | + text_font_size="32pt", |
| 85 | + text_font_style="bold", |
| 86 | + text_align="center", |
| 87 | + text_baseline="middle", |
| 88 | + text_color="#333333", |
| 89 | + ) |
| 90 | + p.add_layout(label) |
| 91 | + # Add set size below the label |
| 92 | + size_label = Label( |
| 93 | + x=lx, |
| 94 | + y=ly - 0.08, |
| 95 | + text=size_text, |
| 96 | + text_font_size="24pt", |
| 97 | + text_align="center", |
| 98 | + text_baseline="middle", |
| 99 | + text_color="#666666", |
| 100 | + ) |
| 101 | + p.add_layout(size_label) |
| 102 | + |
| 103 | +# Add region counts |
| 104 | +# Positions for each region (calculated manually for clarity) |
| 105 | +region_labels = [ |
| 106 | + # (x, y, count, description) |
| 107 | + (-0.35, 0.25, str(only_a), "Only A"), # Only A |
| 108 | + (0.35, 0.25, str(only_b), "Only B"), # Only B |
| 109 | + (0.0, -0.38, str(only_c), "Only C"), # Only C |
| 110 | + (0.0, 0.28, str(only_ab), "A∩B"), # A ∩ B only |
| 111 | + (-0.18, -0.08, str(only_ac), "A∩C"), # A ∩ C only |
| 112 | + (0.18, -0.08, str(only_bc), "B∩C"), # B ∩ C only |
| 113 | + (0.0, 0.05, str(abc), "A∩B∩C"), # A ∩ B ∩ C |
| 114 | +] |
| 115 | + |
| 116 | +for rx, ry, count, _desc in region_labels: |
| 117 | + count_label = Label( |
| 118 | + x=rx, |
| 119 | + y=ry, |
| 120 | + text=count, |
| 121 | + text_font_size="36pt", |
| 122 | + text_font_style="bold", |
| 123 | + text_align="center", |
| 124 | + text_baseline="middle", |
| 125 | + text_color="#222222", |
| 126 | + ) |
| 127 | + p.add_layout(count_label) |
| 128 | + |
| 129 | +# Style the figure - larger title for 4800x2700 canvas |
| 130 | +p.title.text_font_size = "48pt" |
| 131 | +p.title.align = "center" |
| 132 | + |
| 133 | +# Remove axes for cleaner look (Venn diagrams don't need axes) |
| 134 | +p.xaxis.visible = False |
| 135 | +p.yaxis.visible = False |
| 136 | +p.xgrid.visible = False |
| 137 | +p.ygrid.visible = False |
| 138 | +p.outline_line_color = None |
| 139 | + |
| 140 | +# Set background |
| 141 | +p.background_fill_color = "white" |
| 142 | +p.border_fill_color = "white" |
| 143 | + |
| 144 | +# Save as PNG and HTML |
| 145 | +export_png(p, filename="plot.png") |
| 146 | +output_file("plot.html", title="venn-basic · bokeh · pyplots.ai") |
| 147 | +save(p) |
0 commit comments