|
1 | | -""" pyplots.ai |
| 1 | +"""pyplots.ai |
2 | 2 | arc-basic: Basic Arc Diagram |
3 | | -Library: plotnine 0.15.2 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: plotnine 0.15.3 | Python 3.14.3 |
| 4 | +Quality: /100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import sys |
|
13 | 13 | import pandas as pd # noqa: E402 |
14 | 14 | from plotnine import ( # noqa: E402 |
15 | 15 | aes, |
| 16 | + coord_cartesian, |
16 | 17 | element_blank, |
| 18 | + element_rect, |
17 | 19 | element_text, |
18 | 20 | geom_path, |
19 | 21 | geom_point, |
20 | 22 | geom_text, |
21 | 23 | ggplot, |
22 | 24 | labs, |
| 25 | + scale_alpha_identity, |
23 | 26 | scale_size_identity, |
24 | 27 | theme, |
25 | 28 | ) |
26 | 29 |
|
27 | 30 |
|
28 | 31 | # Data: Character interactions in a story chapter |
29 | | -np.random.seed(42) |
30 | | - |
31 | 32 | nodes = ["Alice", "Bob", "Carol", "David", "Eve", "Frank", "Grace", "Henry", "Iris", "Jack"] |
32 | 33 | n_nodes = len(nodes) |
33 | 34 |
|
34 | | -# Edges: pairs of connected nodes with weights (source, target, weight) |
| 35 | +# Edges: (source_idx, target_idx, weight) |
35 | 36 | edges = [ |
36 | | - (0, 1, 3), # Alice-Bob (strong connection) |
| 37 | + (0, 1, 3), # Alice-Bob (strong) |
37 | 38 | (0, 3, 2), # Alice-David |
38 | 39 | (1, 2, 2), # Bob-Carol |
39 | 40 | (2, 4, 1), # Carol-Eve |
|
52 | 53 |
|
53 | 54 | # Node positions along x-axis |
54 | 55 | x_positions = np.linspace(0, 1, n_nodes) |
55 | | -y_baseline = 0.1 |
56 | | - |
57 | | -# Create arc paths dataframe |
58 | | -arc_data = [] |
59 | | -arc_id = 0 |
| 56 | +y_baseline = 0.0 |
60 | 57 |
|
61 | | -for start, end, weight in edges: |
62 | | - x_start = x_positions[start] |
63 | | - x_end = x_positions[end] |
| 58 | +# Build arc paths using vectorized construction |
| 59 | +n_points = 50 |
| 60 | +theta = np.linspace(0, np.pi, n_points) |
| 61 | +arc_rows = [] |
64 | 62 |
|
65 | | - # Arc center and radius |
| 63 | +for arc_id, (start, end, weight) in enumerate(edges): |
| 64 | + x_start, x_end = x_positions[start], x_positions[end] |
66 | 65 | x_center = (x_start + x_end) / 2 |
67 | 66 | arc_radius = abs(x_end - x_start) / 2 |
| 67 | + height = 0.08 * abs(end - start) |
68 | 68 |
|
69 | | - # Arc height proportional to distance between nodes |
70 | | - distance = abs(end - start) |
71 | | - height = 0.08 * distance |
72 | | - |
73 | | - # Generate arc points (semi-circle above baseline) |
74 | | - n_points = 50 |
75 | | - theta = np.linspace(0, np.pi, n_points) |
76 | 69 | x_arc = x_center - arc_radius * np.cos(theta) |
77 | 70 | y_arc = y_baseline + height * np.sin(theta) |
78 | 71 |
|
79 | | - # Add points to dataframe with arc identifier |
80 | | - for i in range(n_points): |
81 | | - arc_data.append( |
82 | | - { |
83 | | - "x": x_arc[i], |
84 | | - "y": y_arc[i], |
85 | | - "arc_id": arc_id, |
86 | | - "weight": weight, |
87 | | - "size": 1.0 + weight * 0.8, # Line thickness based on weight |
88 | | - } |
89 | | - ) |
90 | | - arc_id += 1 |
91 | | - |
92 | | -arc_df = pd.DataFrame(arc_data) |
| 72 | + arc_df_chunk = pd.DataFrame( |
| 73 | + {"x": x_arc, "y": y_arc, "arc_id": arc_id, "size": 0.8 + weight * 0.7, "alpha": 0.30 + weight * 0.18} |
| 74 | + ) |
| 75 | + arc_rows.append(arc_df_chunk) |
93 | 76 |
|
94 | | -# Create nodes dataframe |
95 | | -node_df = pd.DataFrame({"x": x_positions, "y": [y_baseline] * n_nodes, "name": nodes}) |
| 77 | +arc_df = pd.concat(arc_rows, ignore_index=True) |
96 | 78 |
|
97 | | -# Create label dataframe (below nodes) |
98 | | -label_df = pd.DataFrame({"x": x_positions, "y": [y_baseline - 0.05] * n_nodes, "name": nodes}) |
| 79 | +# Node and label dataframes |
| 80 | +node_df = pd.DataFrame({"x": x_positions, "y": [y_baseline] * n_nodes}) |
| 81 | +label_df = pd.DataFrame({"x": x_positions, "y": [y_baseline - 0.03] * n_nodes, "name": nodes}) |
99 | 82 |
|
100 | | -# Create the plot |
| 83 | +# Plot |
101 | 84 | plot = ( |
102 | 85 | ggplot() |
103 | | - # Draw arcs |
104 | | - + geom_path(arc_df, aes(x="x", y="y", group="arc_id", size="size"), color="#306998", alpha=0.6) |
| 86 | + + geom_path(arc_df, aes(x="x", y="y", group="arc_id", size="size", alpha="alpha"), color="#306998") |
105 | 87 | + scale_size_identity() |
106 | | - # Draw nodes |
107 | | - + geom_point(node_df, aes(x="x", y="y"), color="#FFD43B", size=10, stroke=1.5, fill="#FFD43B") |
108 | | - # Add node labels |
109 | | - + geom_text(label_df, aes(x="x", y="y", label="name"), size=11, color="#306998", fontweight="bold", va="top") |
110 | | - + labs(title="Character Interactions · arc-basic · plotnine · pyplots.ai") |
| 88 | + + scale_alpha_identity() |
| 89 | + + geom_point(node_df, aes(x="x", y="y"), color="#306998", size=8, stroke=1.2, fill="white") |
| 90 | + + geom_text(label_df, aes(x="x", y="y", label="name"), size=12, color="#2C3E50", fontweight="bold", va="top") |
| 91 | + + coord_cartesian(xlim=(-0.05, 1.05)) |
| 92 | + + labs(title="Character Interactions \u00b7 arc-basic \u00b7 plotnine \u00b7 pyplots.ai") |
111 | 93 | + theme( |
112 | 94 | figure_size=(16, 9), |
113 | | - plot_title=element_text(size=24, ha="center"), |
| 95 | + plot_title=element_text(size=24, ha="center", weight="bold"), |
| 96 | + plot_margin=0.02, |
114 | 97 | axis_title=element_blank(), |
115 | 98 | axis_text=element_blank(), |
116 | 99 | axis_ticks=element_blank(), |
117 | 100 | panel_grid=element_blank(), |
118 | | - panel_background=element_blank(), |
119 | | - plot_background=element_blank(), |
| 101 | + panel_background=element_rect(fill="white", color="white"), |
| 102 | + plot_background=element_rect(fill="white", color="white"), |
120 | 103 | legend_position="none", |
121 | 104 | ) |
122 | 105 | ) |
123 | 106 |
|
124 | | -# Save the plot |
| 107 | +# Save |
125 | 108 | plot.save("plot.png", dpi=300, verbose=False) |
0 commit comments