Skip to content

Commit 49b4de9

Browse files
committed
Aligned pyvis plot with plotly settings
1 parent a062355 commit 49b4de9

1 file changed

Lines changed: 88 additions & 6 deletions

File tree

src/tdamapper/_plot_pyvis.py

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,93 @@
22
This module provides functionalities to visualize the Mapper graph based on
33
pyvis.
44
"""
5+
import math
56

67
from pyvis.network import Network
78

89
import matplotlib.pyplot as plt
910
import matplotlib.colors as mcolors
1011

12+
import plotly.graph_objects as go
13+
import plotly.io as pio
14+
1115
from tdamapper.core import (
1216
aggregate_graph,
1317
)
1418

1519

20+
_EDGE_WIDTH = 0.75
21+
22+
_EDGE_COLOR = '#777'
23+
24+
25+
def __fmt(x, max_len=3):
26+
fmt = f'.{max_len}g'
27+
return f'{x:{fmt}}'
28+
29+
30+
def _colorbar(width, height, cmap, cmin, cmax, title):
31+
colorbar_fig = go.Figure()
32+
colorbar_fig.add_trace(go.Scatter(
33+
x=[None], y=[None],
34+
mode='markers',
35+
marker=dict(
36+
colorscale=cmap,
37+
cmin=cmin,
38+
cmax=cmax,
39+
colorbar=dict(
40+
title=title,
41+
thickness=20,
42+
len=1.0,
43+
),
44+
),
45+
))
46+
colorbar_fig.update_layout(
47+
xaxis=dict(visible=False),
48+
yaxis=dict(visible=False),
49+
margin=dict(l=50, r=50, t=0, b=0),
50+
#width=width,
51+
height=height,
52+
plot_bgcolor='rgba(0,0,0,0)',
53+
paper_bgcolor='rgba(0,0,0,0)',
54+
)
55+
return colorbar_fig
56+
57+
58+
def _combine(network, colorbar):
59+
network_html = network.generate_html()
60+
colorbar_html = pio.to_html(colorbar, include_plotlyjs='cdn', full_html=False)
61+
combined_html = f"""
62+
<!DOCTYPE html>
63+
<html>
64+
<head>
65+
<title>Network with Colorbar</title>
66+
<style>
67+
body {{
68+
display: flex;
69+
flex-direction: row;
70+
margin: 0;
71+
padding: 0;
72+
height: 100vh;
73+
}}
74+
.network {{
75+
flex: 3;
76+
}}
77+
</style>
78+
</head>
79+
<body>
80+
<div class="network">
81+
{network_html}
82+
</div>
83+
<div class="colorbar">
84+
{colorbar_html}
85+
</div>
86+
</body>
87+
</html>
88+
"""
89+
return combined_html
90+
91+
1692
def plot_pyvis(
1793
mapper_plot,
1894
notebook,
@@ -81,10 +157,10 @@ def _compute_net(
81157

82158
def _size(node):
83159
if max_node_size == min_node_size:
84-
node_size_norm = 12
160+
node_size_norm = 25.0
85161
else:
86162
node_size = int(nodes[node]['size'])
87-
node_size_norm = (node_size - min_node_size) / (max_node_size - min_node_size) * 25
163+
node_size_norm = 25.0 * math.sqrt(node_size / max_node_size)
88164
return int(round(node_size_norm))
89165

90166
def _color(node):
@@ -112,20 +188,26 @@ def _blend_color(source, target):
112188
node_id = int(node)
113189
node_size = _size(node)
114190
node_color = _color(node)
191+
node_stats = __fmt(node_colors[node])
192+
node_label = f'color: {node_stats}\nnode: {node_id}\nsize: {node_size}'
115193
node_pos = mapper_plot.positions[node]
116194
net.add_node(
117195
node_id,
118196
label=node_id,
119197
size=node_size,
120198
color=node_color,
121-
x=node_pos[0] * 1000.0,
122-
y=node_pos[1] * 1000.0,
199+
title=node_label,
200+
x=node_pos[0] * width,
201+
y=node_pos[1] * height,
123202
)
124203

125204
for edge in graph.edges:
126205
source_id = int(edge[0])
127206
target_id = int(edge[1])
128-
edge_color = _blend_color(edge[0], edge[1])
129-
net.add_edge(source_id, target_id, color=edge_color)
207+
#edge_color = _blend_color(edge[0], edge[1])
208+
edge_color = _EDGE_COLOR
209+
edge_width = _EDGE_WIDTH
210+
edge_width = 1.5
211+
net.add_edge(source_id, target_id, color=edge_color, width=edge_width)
130212

131213
return net

0 commit comments

Comments
 (0)