|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | arc-basic: Basic Arc Diagram |
3 | | -Library: altair 6.0.0 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: altair 6.0.0 | Python 3.14.3 |
| 4 | +Quality: /100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import altair as alt |
|
15 | 15 | nodes = ["Alice", "Bob", "Carol", "David", "Eve", "Frank", "Grace", "Henry", "Iris", "Jack"] |
16 | 16 | n_nodes = len(nodes) |
17 | 17 |
|
18 | | -# Edges: pairs of connected nodes with weights |
19 | 18 | edges = [ |
20 | | - (0, 1, 3), # Alice-Bob (strong connection) |
| 19 | + (0, 1, 3), # Alice-Bob (strong) |
21 | 20 | (0, 3, 2), # Alice-David |
22 | 21 | (1, 2, 2), # Bob-Carol |
23 | 22 | (2, 4, 1), # Carol-Eve |
|
36 | 35 |
|
37 | 36 | # Node positions along x-axis |
38 | 37 | x_positions = np.linspace(0, 100, n_nodes) |
39 | | -y_baseline = 10 |
| 38 | +y_baseline = 0 |
40 | 39 |
|
41 | | -# Create node dataframe |
42 | | -nodes_df = pd.DataFrame({"x": x_positions, "y": [y_baseline] * n_nodes, "name": nodes}) |
| 40 | +# Node dataframe with connection count for sizing |
| 41 | +connection_count = [0] * n_nodes |
| 42 | +for s, e, w in edges: |
| 43 | + connection_count[s] += w |
| 44 | + connection_count[e] += w |
43 | 45 |
|
44 | | -# Create arc paths - each arc as a series of points following a semicircle |
| 46 | +nodes_df = pd.DataFrame({"x": x_positions, "y": [y_baseline] * n_nodes, "name": nodes, "connections": connection_count}) |
| 47 | + |
| 48 | +# Build arc paths as semicircular curves |
45 | 49 | arc_data = [] |
46 | 50 | points_per_arc = 50 |
| 51 | +max_span = max(abs(e - s) for s, e, _ in edges) |
47 | 52 |
|
48 | 53 | for edge_id, (start, end, weight) in enumerate(edges): |
49 | 54 | x_start = x_positions[start] |
50 | 55 | x_end = x_positions[end] |
| 56 | + span = abs(end - start) |
| 57 | + height = 7 * span |
51 | 58 |
|
52 | | - # Arc height proportional to distance between nodes |
53 | | - distance = abs(end - start) |
54 | | - height = 8 * distance |
55 | | - |
56 | | - # Generate points along a semicircle arc |
57 | 59 | angles = np.linspace(0, np.pi, points_per_arc) |
58 | | - |
59 | 60 | x_center = (x_start + x_end) / 2 |
60 | 61 | radius_x = abs(x_end - x_start) / 2 |
61 | 62 | radius_y = height / 2 |
62 | 63 |
|
| 64 | + pair = f"{nodes[start]}–{nodes[end]}" |
63 | 65 | for i, angle in enumerate(angles): |
64 | 66 | arc_data.append( |
65 | 67 | { |
66 | 68 | "edge_id": edge_id, |
67 | 69 | "x": x_center - radius_x * np.cos(angle), |
68 | 70 | "y": y_baseline + radius_y * np.sin(angle), |
69 | 71 | "weight": weight, |
| 72 | + "pair": pair, |
70 | 73 | "order": i, |
71 | 74 | } |
72 | 75 | ) |
73 | 76 |
|
74 | 77 | arcs_df = pd.DataFrame(arc_data) |
75 | 78 |
|
76 | | -# Create arc chart with lines |
| 79 | +# Y-domain: tight around data for better canvas use |
| 80 | +max_arc_height = 7 * max_span / 2 |
| 81 | +y_domain = [-4, max_arc_height + 4] |
| 82 | + |
| 83 | +# Arcs: weight drives both thickness and opacity for visual hierarchy |
77 | 84 | arcs = ( |
78 | 85 | alt.Chart(arcs_df) |
79 | | - .mark_line(strokeWidth=2, opacity=0.6) |
| 86 | + .mark_line() |
80 | 87 | .encode( |
81 | 88 | x=alt.X("x:Q", axis=None), |
82 | | - y=alt.Y("y:Q", axis=None), |
| 89 | + y=alt.Y("y:Q", axis=None, scale=alt.Scale(domain=y_domain)), |
83 | 90 | detail="edge_id:N", |
84 | | - strokeWidth=alt.StrokeWidth("weight:Q", scale=alt.Scale(domain=[1, 3], range=[2, 6]), legend=None), |
| 91 | + strokeWidth=alt.StrokeWidth( |
| 92 | + "weight:Q", |
| 93 | + scale=alt.Scale(domain=[1, 3], range=[1.5, 5]), |
| 94 | + legend=alt.Legend( |
| 95 | + title="Interaction Strength", |
| 96 | + titleFontSize=16, |
| 97 | + labelFontSize=14, |
| 98 | + orient="top-right", |
| 99 | + offset=10, |
| 100 | + values=[1, 2, 3], |
| 101 | + symbolStrokeWidth=3, |
| 102 | + labelExpr="datum.value == 1 ? 'Weak' : datum.value == 2 ? 'Moderate' : 'Strong'", |
| 103 | + ), |
| 104 | + ), |
| 105 | + strokeOpacity=alt.StrokeOpacity("weight:Q", scale=alt.Scale(domain=[1, 3], range=[0.35, 0.7]), legend=None), |
85 | 106 | color=alt.value("#306998"), |
| 107 | + tooltip=[alt.Tooltip("pair:N", title="Connection"), alt.Tooltip("weight:Q", title="Strength")], |
86 | 108 | ) |
87 | | - .properties(width=1600, height=900) |
88 | 109 | ) |
89 | 110 |
|
90 | | -# Create node points |
| 111 | +# Nodes: size proportional to total connection weight |
91 | 112 | node_points = ( |
92 | 113 | alt.Chart(nodes_df) |
93 | | - .mark_circle(size=600, color="#FFD43B", stroke="#306998", strokeWidth=3) |
94 | | - .encode(x=alt.X("x:Q", axis=None), y=alt.Y("y:Q", axis=None)) |
| 114 | + .mark_circle(color="#FFD43B", stroke="#306998", strokeWidth=2.5) |
| 115 | + .encode( |
| 116 | + x=alt.X("x:Q", axis=None), |
| 117 | + y=alt.Y("y:Q", axis=None, scale=alt.Scale(domain=y_domain)), |
| 118 | + size=alt.Size("connections:Q", scale=alt.Scale(domain=[2, 11], range=[300, 800]), legend=None), |
| 119 | + tooltip=[alt.Tooltip("name:N", title="Character"), alt.Tooltip("connections:Q", title="Total Weight")], |
| 120 | + ) |
95 | 121 | ) |
96 | 122 |
|
97 | | -# Create node labels |
| 123 | +# Node labels below baseline |
98 | 124 | node_labels = ( |
99 | 125 | alt.Chart(nodes_df) |
100 | | - .mark_text(dy=30, fontSize=18, fontWeight="bold", color="#306998") |
| 126 | + .mark_text(dy=26, fontSize=18, fontWeight="bold", color="#306998") |
101 | 127 | .encode(x=alt.X("x:Q"), y=alt.Y("y:Q"), text="name:N") |
102 | 128 | ) |
103 | 129 |
|
|
107 | 133 | .properties( |
108 | 134 | width=1600, |
109 | 135 | height=900, |
110 | | - title=alt.Title("Character Interactions · arc-basic · altair · pyplots.ai", fontSize=28, anchor="middle"), |
| 136 | + title=alt.Title( |
| 137 | + "Character Interactions · arc-basic · altair · pyplots.ai", fontSize=28, anchor="middle", offset=15 |
| 138 | + ), |
111 | 139 | ) |
112 | 140 | .configure_view(strokeWidth=0) |
| 141 | + .configure_legend(strokeColor="transparent", padding=12) |
113 | 142 | ) |
114 | 143 |
|
115 | | -# Save outputs |
| 144 | +# Save |
116 | 145 | chart.save("plot.png", scale_factor=3.0) |
117 | 146 | chart.save("plot.html") |
0 commit comments