Skip to content

Commit 03e6be3

Browse files
committed
Fixed html styling, coordinates and colormap
1 parent 1751d05 commit 03e6be3

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

src/tdamapper/_plot_pyvis.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import plotly.graph_objects as go
1313
import plotly.io as pio
14+
import plotly.colors as pc
1415

1516
from tdamapper.core import (
1617
aggregate_graph,
@@ -21,6 +22,8 @@
2122

2223
_EDGE_COLOR = '#777'
2324

25+
_TICKS_NUM = 10
26+
2427

2528
def __fmt(x, max_len=3):
2629
fmt = f'.{max_len}g'
@@ -33,21 +36,34 @@ def _colorbar(height, cmap, cmin, cmax, title):
3336
x=[None], y=[None],
3437
mode='markers',
3538
marker=dict(
39+
showscale=True,
40+
reversescale=False,
41+
line_colorscale=cmap,
3642
colorscale=cmap,
3743
cmin=cmin,
3844
cmax=cmax,
3945
colorbar=dict(
40-
title=title,
46+
showticklabels=True,
47+
outlinewidth=1,
48+
borderwidth=0,
49+
orientation='v',
4150
thickness=20,
42-
len=1.0,
51+
thicknessmode='fraction',
52+
xanchor='left',
53+
titleside='right',
54+
tickwidth=1,
55+
tickformat='.2g',
56+
nticks=_TICKS_NUM,
57+
tickmode='auto',
58+
title=title,
4359
),
4460
),
4561
))
4662
colorbar_fig.update_layout(
4763
xaxis=dict(visible=False),
4864
yaxis=dict(visible=False),
49-
margin=dict(l=50, r=50, t=0, b=0),
50-
width=200,
65+
margin=dict(l=0, r=100, t=0, b=0),
66+
width=80,
5167
height=height,
5268
plot_bgcolor='rgba(0,0,0,0)',
5369
paper_bgcolor='rgba(0,0,0,0)',
@@ -57,7 +73,14 @@ def _colorbar(height, cmap, cmin, cmax, title):
5773

5874
def _combine(network, colorbar):
5975
network_html = network.generate_html()
60-
colorbar_html = pio.to_html(colorbar, include_plotlyjs='cdn', full_html=False, config={'displayModeBar': False})
76+
colorbar_html = pio.to_html(
77+
colorbar,
78+
include_plotlyjs='cdn',
79+
full_html=False,
80+
config={
81+
'displayModeBar': False
82+
},
83+
)
6184
combined_html = f"""
6285
<!DOCTYPE html>
6386
<html>
@@ -70,43 +93,33 @@ def _combine(network, colorbar):
7093
display: flex;
7194
}}
7295
73-
.container {{
74-
flex: 1;
96+
.combined {{
7597
display: flex;
7698
flex-direction: row;
99+
margin: 0;
77100
}}
78101
79102
.network {{
80-
flex: 3;
81103
display: flex;
82-
justify-content: center;
83104
align-items: start;
84-
background-color: #f0f0f0;
85-
height: 100%;
86-
}}
87-
88-
.network-content {{
89-
width: 100%;
90-
height: 100%;
91105
}}
92106
93107
.colorbar {{
94-
flex: 3;
95108
display: flex;
96-
justify-content: center;
97109
align-items: start;
98-
background-color: #f0f0f0;
99-
height: 100%;
100110
}}
101111
102-
.colorbar-content {{
103-
width: 100%;
104-
height: 100%;
112+
.network .card {{
113+
border-width: 0;
114+
}}
115+
116+
.network #mynetwork {{
117+
border-width: 0;
105118
}}
106119
</style>
107120
</head>
108121
<body>
109-
<div class="container">
122+
<div class="combined">
110123
<div class="network">
111124
{network_html}
112125
</div>
@@ -118,7 +131,6 @@ def _combine(network, colorbar):
118131
</html>
119132
"""
120133
return combined_html
121-
#return network_html
122134

123135

124136
def plot_pyvis(
@@ -171,6 +183,7 @@ def _compute_net(
171183
net.toggle_physics(False)
172184
graph = mapper_plot.graph
173185
nodes = graph.nodes
186+
cmap_colorscale = pc.get_colorscale(cmap)
174187

175188
min_node_size = float('inf')
176189
max_node_size = -float('inf')
@@ -207,20 +220,9 @@ def _color(node):
207220
else:
208221
node_color = node_colors[node]
209222
node_color = (node_color - min_node_color) / (max_node_color - min_node_color)
210-
node_color = colormap(node_color)
211-
return mcolors.to_hex(node_color)
212-
213-
def _blend_color(source, target):
214-
if max_node_color == min_node_color:
215-
blend_color = 0.5
216-
else:
217-
source_color = node_colors[source]
218-
source_color = (source_color - min_node_color) / (max_node_color - min_node_color)
219-
target_color = node_colors[target]
220-
target_color = (target_color - min_node_color) / (max_node_color - min_node_color)
221-
blend_color = (source_color + target_color) / 2.0
222-
blend_color = colormap(blend_color)
223-
return mcolors.to_hex(blend_color)
223+
node_color = max(0.0, min(1.0, node_color))
224+
node_color_hex = pc.sample_colorscale(cmap_colorscale, node_color)[0]
225+
return node_color_hex
224226

225227
for node in nodes:
226228
node_id = int(node)
@@ -236,13 +238,12 @@ def _blend_color(source, target):
236238
color=node_color,
237239
title=node_label,
238240
x=node_pos[0] * width,
239-
y=node_pos[1] * height,
241+
y=-node_pos[1] * height,
240242
)
241243

242244
for edge in graph.edges:
243245
source_id = int(edge[0])
244246
target_id = int(edge[1])
245-
#edge_color = _blend_color(edge[0], edge[1])
246247
edge_color = _EDGE_COLOR
247248
edge_width = _EDGE_WIDTH
248249
edge_width = 1.5

0 commit comments

Comments
 (0)