Skip to content

Commit 74ee69a

Browse files
committed
com
1 parent 64cd49b commit 74ee69a

1 file changed

Lines changed: 140 additions & 9 deletions

File tree

docs/gallery/networks.ipynb

Lines changed: 140 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,37 @@
2828
"id": "cell-2",
2929
"metadata": {},
3030
"outputs": [],
31-
"source": "import numpy as np\nimport pandas as pd\nfrom ggplotly import *\n\n# Create nodes in a circular layout\nn_nodes = 20\nangles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False)\nradius = 10\nnode_x = radius * np.cos(angles)\nnode_y = radius * np.sin(angles)\n\n# Create edges across the circle\nedges = []\nfor i in range(n_nodes):\n for offset in [5, 7, 10]:\n j = (i + offset) % n_nodes\n edges.append({\n 'x': node_x[i], 'y': node_y[i],\n 'xend': node_x[j], 'yend': node_y[j]\n })\n\nedges_df = pd.DataFrame(edges)\nnodes_df = pd.DataFrame({'x': node_x, 'y': node_y})\n\n(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend'))\n + geom_edgebundle(compatibility_threshold=0.6)\n + geom_point(data=nodes_df, mapping=aes(x='x', y='y'), color='white', size=4)\n + theme_dark()\n + labs(title='Circular Network with Edge Bundling'))"
31+
"source": [
32+
"import numpy as np\n",
33+
"import pandas as pd\n",
34+
"from ggplotly import *\n",
35+
"\n",
36+
"# Create nodes in a circular layout\n",
37+
"n_nodes = 20\n",
38+
"angles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False)\n",
39+
"radius = 10\n",
40+
"node_x = radius * np.cos(angles)\n",
41+
"node_y = radius * np.sin(angles)\n",
42+
"\n",
43+
"# Create edges across the circle\n",
44+
"edges = []\n",
45+
"for i in range(n_nodes):\n",
46+
" for offset in [5, 7, 10]:\n",
47+
" j = (i + offset) % n_nodes\n",
48+
" edges.append({\n",
49+
" 'x': node_x[i], 'y': node_y[i],\n",
50+
" 'xend': node_x[j], 'yend': node_y[j]\n",
51+
" })\n",
52+
"\n",
53+
"edges_df = pd.DataFrame(edges)\n",
54+
"nodes_df = pd.DataFrame({'x': node_x, 'y': node_y})\n",
55+
"\n",
56+
"(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend'))\n",
57+
" + geom_edgebundle(compatibility_threshold=0.6)\n",
58+
" + geom_point(data=nodes_df, mapping=aes(x='x', y='y'), color='white', size=4)\n",
59+
" + theme_dark()\n",
60+
" + labs(title='Circular Network with Edge Bundling'))"
61+
]
3262
},
3363
{
3464
"cell_type": "markdown",
@@ -44,7 +74,31 @@
4474
"id": "cell-4",
4575
"metadata": {},
4676
"outputs": [],
47-
"source": "np.random.seed(42)\nn_nodes = 30\nn_edges = 80\n\nnode_x = np.random.uniform(0, 100, n_nodes)\nnode_y = np.random.uniform(0, 100, n_nodes)\n\nedges = []\nfor _ in range(n_edges):\n i, j = np.random.choice(n_nodes, 2, replace=False)\n edges.append({\n 'x': node_x[i], 'y': node_y[i],\n 'xend': node_x[j], 'yend': node_y[j]\n })\n\nedges_df = pd.DataFrame(edges)\nnodes_df = pd.DataFrame({'x': node_x, 'y': node_y})\n\n(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend'))\n + geom_edgebundle(C=5, compatibility_threshold=0.5)\n + geom_point(data=nodes_df, mapping=aes(x='x', y='y'), color='#00ff00', size=5)\n + theme_dark()\n + labs(title='Random Network with Edge Bundling'))"
77+
"source": [
78+
"np.random.seed(42)\n",
79+
"n_nodes = 30\n",
80+
"n_edges = 80\n",
81+
"\n",
82+
"node_x = np.random.uniform(0, 100, n_nodes)\n",
83+
"node_y = np.random.uniform(0, 100, n_nodes)\n",
84+
"\n",
85+
"edges = []\n",
86+
"for _ in range(n_edges):\n",
87+
" i, j = np.random.choice(n_nodes, 2, replace=False)\n",
88+
" edges.append({\n",
89+
" 'x': node_x[i], 'y': node_y[i],\n",
90+
" 'xend': node_x[j], 'yend': node_y[j]\n",
91+
" })\n",
92+
"\n",
93+
"edges_df = pd.DataFrame(edges)\n",
94+
"nodes_df = pd.DataFrame({'x': node_x, 'y': node_y})\n",
95+
"\n",
96+
"(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend'))\n",
97+
" + geom_edgebundle(C=5, compatibility_threshold=0.5)\n",
98+
" + geom_point(data=nodes_df, mapping=aes(x='x', y='y'), color='#00ff00', size=5)\n",
99+
" + theme_dark()\n",
100+
" + labs(title='Random Network with Edge Bundling'))"
101+
]
48102
},
49103
{
50104
"cell_type": "markdown",
@@ -81,7 +135,20 @@
81135
"id": "cell-6",
82136
"metadata": {},
83137
"outputs": [],
84-
"source": "# Edges with weights - heavier edges attract lighter ones\nedges_df = pd.DataFrame({\n 'x': [0, 0, 0],\n 'y': [0, 1, 2],\n 'xend': [10, 10, 10],\n 'yend': [0, 1, 2],\n 'traffic': [100, 10, 10] # First edge is 10x heavier\n})\n\n(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend', weight='traffic'))\n + geom_edgebundle(C=4, compatibility_threshold=0.5)\n + theme_dark())"
138+
"source": [
139+
"# Edges with weights - heavier edges attract lighter ones\n",
140+
"edges_df = pd.DataFrame({\n",
141+
" 'x': [0, 0, 0],\n",
142+
" 'y': [0, 1, 2],\n",
143+
" 'xend': [10, 10, 10],\n",
144+
" 'yend': [0, 1, 2],\n",
145+
" 'traffic': [100, 10, 10] # First edge is 10x heavier\n",
146+
"})\n",
147+
"\n",
148+
"(ggplot(edges_df, aes(x='x', y='y', xend='xend', yend='yend', weight='traffic'))\n",
149+
" + geom_edgebundle(C=4, compatibility_threshold=0.5)\n",
150+
" + theme_dark())"
151+
]
85152
},
86153
{
87154
"cell_type": "markdown",
@@ -99,7 +166,19 @@
99166
"id": "cell-8",
100167
"metadata": {},
101168
"outputs": [],
102-
"source": "import igraph as ig\nfrom ggplotly import data\n\n# Load built-in US flights network\ng = data('us_flights')\n\n(ggplot()\n + geom_map(map_type='usa')\n + geom_edgebundle(graph=g, show_nodes=True, node_color='white', node_size=3)\n + theme_dark()\n + labs(title='US Flight Network'))"
169+
"source": [
170+
"import igraph as ig\n",
171+
"from ggplotly import data\n",
172+
"\n",
173+
"# Load built-in US flights network\n",
174+
"g = data('us_flights')\n",
175+
"\n",
176+
"(ggplot()\n",
177+
" + geom_map(map_type='usa')\n",
178+
" + geom_edgebundle(graph=g, show_nodes=True, node_color='white', node_size=3)\n",
179+
" + theme_dark()\n",
180+
" + labs(title='US Flight Network'))"
181+
]
103182
},
104183
{
105184
"cell_type": "markdown",
@@ -119,7 +198,27 @@
119198
"id": "cell-10",
120199
"metadata": {},
121200
"outputs": [],
122-
"source": "airports = pd.DataFrame({\n 'lon': [-122.4, -73.8, -87.6, -118.4, -95.3, -84.4],\n 'lat': [37.8, 40.6, 41.9, 34.0, 29.8, 33.6],\n 'name': ['SFO', 'JFK', 'ORD', 'LAX', 'IAH', 'ATL']\n})\n\nflights = pd.DataFrame({\n 'src_lon': [-122.4, -73.8, -87.6, -118.4, -95.3, -84.4, -122.4, -73.8],\n 'src_lat': [37.8, 40.6, 41.9, 34.0, 29.8, 33.6, 37.8, 40.6],\n 'dst_lon': [-73.8, -87.6, -118.4, -95.3, -84.4, -122.4, -84.4, -118.4],\n 'dst_lat': [40.6, 41.9, 34.0, 29.8, 33.6, 37.8, 33.6, 34.0]\n})\n\n(ggplot(flights, aes(x='src_lon', y='src_lat', xend='dst_lon', yend='dst_lat'))\n + geom_map(map_type='usa')\n + geom_point(data=airports, mapping=aes(x='lon', y='lat'), color='white', size=8)\n + geom_edgebundle(C=4, compatibility_threshold=0.5, verbose=False)\n + theme_dark()\n + labs(title='US Flights with Edge Bundling'))"
201+
"source": [
202+
"airports = pd.DataFrame({\n",
203+
" 'lon': [-122.4, -73.8, -87.6, -118.4, -95.3, -84.4],\n",
204+
" 'lat': [37.8, 40.6, 41.9, 34.0, 29.8, 33.6],\n",
205+
" 'name': ['SFO', 'JFK', 'ORD', 'LAX', 'IAH', 'ATL']\n",
206+
"})\n",
207+
"\n",
208+
"flights = pd.DataFrame({\n",
209+
" 'src_lon': [-122.4, -73.8, -87.6, -118.4, -95.3, -84.4, -122.4, -73.8],\n",
210+
" 'src_lat': [37.8, 40.6, 41.9, 34.0, 29.8, 33.6, 37.8, 40.6],\n",
211+
" 'dst_lon': [-73.8, -87.6, -118.4, -95.3, -84.4, -122.4, -84.4, -118.4],\n",
212+
" 'dst_lat': [40.6, 41.9, 34.0, 29.8, 33.6, 37.8, 33.6, 34.0]\n",
213+
"})\n",
214+
"\n",
215+
"(ggplot(flights, aes(x='src_lon', y='src_lat', xend='dst_lon', yend='dst_lat'))\n",
216+
" + geom_map(map_type='usa')\n",
217+
" + geom_point(data=airports, mapping=aes(x='lon', y='lat'), color='white', size=8)\n",
218+
" + geom_edgebundle(C=4, compatibility_threshold=0.5, verbose=False)\n",
219+
" + theme_dark()\n",
220+
" + labs(title='US Flights with Edge Bundling'))"
221+
]
123222
},
124223
{
125224
"cell_type": "markdown",
@@ -141,7 +240,21 @@
141240
"id": "cell-12",
142241
"metadata": {},
143242
"outputs": [],
144-
"source": "shipping_routes = pd.DataFrame({\n 'origin': ['Rotterdam', 'Shanghai', 'Los Angeles'],\n 'x': [4.48, 121.47, -118.24], # origin longitude\n 'y': [51.92, 31.23, 33.73], # origin latitude\n 'xend': [121.47, -74.01, 4.48], # destination longitude\n 'yend': [31.23, 40.71, 51.92] # destination latitude\n})\n\n(ggplot(shipping_routes, aes(x='x', y='y', xend='xend', yend='yend'))\n + geom_map(map_type='world')\n + geom_searoute(color='steelblue', linewidth=1.0)\n + theme_dark()\n + labs(title='Maritime Shipping Routes'))"
243+
"source": [
244+
"shipping_routes = pd.DataFrame({\n",
245+
" 'origin': ['Rotterdam', 'Shanghai', 'Los Angeles'],\n",
246+
" 'x': [4.48, 121.47, -118.24], # origin longitude\n",
247+
" 'y': [51.92, 31.23, 33.73], # origin latitude\n",
248+
" 'xend': [121.47, -74.01, 4.48], # destination longitude\n",
249+
" 'yend': [31.23, 40.71, 51.92] # destination latitude\n",
250+
"})\n",
251+
"\n",
252+
"(ggplot(shipping_routes, aes(x='x', y='y', xend='xend', yend='yend'))\n",
253+
" + geom_map(map_type='world')\n",
254+
" + geom_searoute(color='steelblue', linewidth=1.0)\n",
255+
" + theme_dark()\n",
256+
" + labs(title='Maritime Shipping Routes'))"
257+
]
145258
},
146259
{
147260
"cell_type": "markdown",
@@ -159,7 +272,20 @@
159272
"id": "cell-14",
160273
"metadata": {},
161274
"outputs": [],
162-
"source": "(ggplot(shipping_routes, aes(x='x', y='y', xend='xend', yend='yend'))\n + geom_map(map_type='world')\n + geom_searoute(\n restrictions=['suez'], # Routes go around Cape of Good Hope\n color='#ff6b35',\n show_highlight=True,\n show_ports=True,\n port_color='#00ff88',\n verbose=True # Shows route distances\n )\n + theme_dark()\n + labs(title='Shipping Routes Avoiding Suez Canal'))"
275+
"source": [
276+
"(ggplot(shipping_routes, aes(x='x', y='y', xend='xend', yend='yend'))\n",
277+
" + geom_map(map_type='world')\n",
278+
" + geom_searoute(\n",
279+
" restrictions=['suez'], \n",
280+
" color='#ff6b35',\n",
281+
" show_highlight=True,\n",
282+
" show_ports=True,\n",
283+
" port_color='#00ff88',\n",
284+
" verbose=True # Shows route distances\n",
285+
" )\n",
286+
" + theme_dark()\n",
287+
" + labs(title='Shipping Routes Avoiding Suez Canal'))"
288+
]
163289
},
164290
{
165291
"cell_type": "markdown",
@@ -190,7 +316,12 @@
190316
"id": "cell-16",
191317
"metadata": {},
192318
"outputs": [],
193-
"source": "from ggplotly.stats.stat_edgebundle import clear_bundling_cache\n\n# Clear cache if needed (e.g., memory constraints)\nclear_bundling_cache()"
319+
"source": [
320+
"from ggplotly.stats.stat_edgebundle import clear_bundling_cache\n",
321+
"\n",
322+
"# Clear cache if needed (e.g., memory constraints)\n",
323+
"clear_bundling_cache()"
324+
]
194325
},
195326
{
196327
"cell_type": "markdown",
@@ -217,4 +348,4 @@
217348
},
218349
"nbformat": 4,
219350
"nbformat_minor": 5
220-
}
351+
}

0 commit comments

Comments
 (0)