-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmy_networkx.py
More file actions
169 lines (145 loc) · 4.9 KB
/
Copy pathmy_networkx.py
File metadata and controls
169 lines (145 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def my_draw_networkx_edge_labels(
G,
pos,
edge_labels=None,
label_pos=0.5,
font_size=10,
font_color="k",
font_family="sans-serif",
font_weight="normal",
alpha=None,
bbox=None,
horizontalalignment="center",
verticalalignment="center",
ax=None,
rotate=True,
clip_on=True,
rad=0
):
# """Draw edge labels.
# Parameters
# ----------
# G : graph
# A networkx graph
# pos : dictionary
# A dictionary with nodes as keys and positions as values.
# Positions should be sequences of length 2.
# edge_labels : dictionary (default={})
# Edge labels in a dictionary of labels keyed by edge two-tuple.
# Only labels for the keys in the dictionary are drawn.
# label_pos : float (default=0.5)
# Position of edge label along edge (0=head, 0.5=center, 1=tail)
# font_size : int (default=10)
# Font size for text labels
# font_color : string (default='k' black)
# Font color string
# font_weight : string (default='normal')
# Font weight
# font_family : string (default='sans-serif')
# Font family
# alpha : float or None (default=None)
# The text transparency
# bbox : Matplotlib bbox, optional
# Specify text box properties (e.g. shape, color etc.) for edge labels.
# Default is {boxstyle='round', ec=(1.0, 1.0, 1.0), fc=(1.0, 1.0, 1.0)}.
# horizontalalignment : string (default='center')
# Horizontal alignment {'center', 'right', 'left'}
# verticalalignment : string (default='center')
# Vertical alignment {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
# ax : Matplotlib Axes object, optional
# Draw the graph in the specified Matplotlib axes.
# rotate : bool (deafult=True)
# Rotate edge labels to lie parallel to edges
# clip_on : bool (default=True)
# Turn on clipping of edge labels at axis boundaries
# Returns
# -------
# dict
# `dict` of labels keyed by edge
# Examples
# --------
# >>> G = nx.dodecahedral_graph()
# >>> edge_labels = nx.draw_networkx_edge_labels(G, pos=nx.spring_layout(G))
# Also see the NetworkX drawing examples at
# https://networkx.org/documentation/latest/auto_examples/index.html
# See Also
# --------
# draw
# draw_networkx
# draw_networkx_nodes
# draw_networkx_edges
# draw_networkx_labels
# """
import matplotlib.pyplot as plt
import numpy as np
if ax is None:
ax = plt.gca()
if edge_labels is None:
labels = {(u, v): d for u, v, d in G.edges(data=True)}
else:
labels = edge_labels
text_items = {}
for (n1, n2), label in labels.items():
(x1, y1) = pos[n1]
(x2, y2) = pos[n2]
(x, y) = (
x1 * label_pos + x2 * (1.0 - label_pos),
y1 * label_pos + y2 * (1.0 - label_pos),
)
pos_1 = ax.transData.transform(np.array(pos[n1]))
pos_2 = ax.transData.transform(np.array(pos[n2]))
linear_mid = 0.5*pos_1 + 0.5*pos_2
d_pos = pos_2 - pos_1
rotation_matrix = np.array([(0,1), (-1,0)])
ctrl_1 = linear_mid + rad*rotation_matrix@d_pos
ctrl_mid_1 = 0.5*pos_1 + 0.5*ctrl_1
ctrl_mid_2 = 0.5*pos_2 + 0.5*ctrl_1
bezier_mid = 0.5*ctrl_mid_1 + 0.5*ctrl_mid_2
(x, y) = ax.transData.inverted().transform(bezier_mid)
if rotate:
# in degrees
angle = np.arctan2(y2 - y1, x2 - x1) / (2.0 * np.pi) * 360
# make label orientation "right-side-up"
if angle > 90:
angle -= 180
if angle < -90:
angle += 180
# transform data coordinate angle to screen coordinate angle
xy = np.array((x, y))
trans_angle = ax.transData.transform_angles(
np.array((angle,)), xy.reshape((1, 2))
)[0]
else:
trans_angle = 0.0
# use default box of white with white border
if bbox is None:
bbox = dict(boxstyle="round", ec=(1.0, 1.0, 1.0), fc=(1.0, 1.0, 1.0))
if not isinstance(label, str):
label = str(label) # this makes "1" and 1 labeled the same
t = ax.text(
x,
y,
label,
size=font_size,
color=font_color,
family=font_family,
weight=font_weight,
alpha=alpha,
horizontalalignment=horizontalalignment,
verticalalignment=verticalalignment,
rotation=trans_angle,
transform=ax.transData,
bbox=bbox,
zorder=1,
clip_on=clip_on,
)
text_items[(n1, n2)] = t
ax.tick_params(
axis="both",
which="both",
bottom=False,
left=False,
labelbottom=False,
labelleft=False,
)
return text_items