Skip to content

Commit

Permalink
add top placement for node labels
Browse files Browse the repository at this point in the history
  • Loading branch information
wspr committed May 29, 2024
1 parent 70afbdc commit d14becd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
31 changes: 23 additions & 8 deletions ausankey/ausankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class Sankey:
label_loc : [str1, str2, str3]
Position to place labels next to the nodes.
* `str1`: position of first labels (`"left"`, `"right"`, `"center"`, or `"none"`)
* `str2`: position of middle labels (`"left"`, `"right"`, `"both"`, `"center"`, or `"none"`)
* `str3`: position of last labels (`"left"`, `"right"`, `"center"`, or `"none"`)
* `str1`: position of first labels (`"left"`, `"right"`, `"center"`, `"top"`, or `"none"`)
* `str2`: position of middle labels (`"left"`, `"right"`, `"both"`, `"center"`, `"top"`, or `"none"`)
* `str3`: position of last labels (`"left"`, `"right"`, `"center"`, `"top"`, or `"none"`)
label_duplicate : bool
When set False, will only print a middle label if that label didn't
Expand Down Expand Up @@ -527,6 +527,7 @@ def calc_plot_dimens(self):
self.y_node_gap = self.node_gap * self.plot_height_nom
self.y_title_gap = self.title_gap * self.plot_height_nom
self.y_frame_gap = self.frame_gap * self.plot_height_nom
self.y_label_gap = self.label_gap * self.plot_height_nom

# horizontal positions
self.x_node_width = self.node_width * self.plot_width_nom
Expand Down Expand Up @@ -587,7 +588,7 @@ def subplot(self, ii):

# Draw node labels

ha_dict = {"left": "right", "right": "left", "center": "center"}
ha_dict = {"left": "right", "right": "left", "center": "center", "top": "center"}

# first row of labels
lr = 0
Expand All @@ -596,10 +597,13 @@ def subplot(self, ii):
xx = x_lr[lr] - self.x_label_gap - self.x_node_width
elif self.label_loc[0] in ("right"):
xx = x_lr[lr] + self.x_label_gap
elif self.label_loc[0] in ("center"):
elif self.label_loc[0] in ("center", "top"):
xx = x_lr[lr] - self.x_node_width / 2
for label in self.node_sizes[ii + lr]:
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] / 2
if self.label_loc[0] in ("top"):
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] + self.y_label_gap
else:
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] / 2
self.draw_label(xx, yy, label, ha_dict[self.label_loc[0]])

# inside labels, left
Expand All @@ -619,6 +623,14 @@ def subplot(self, ii):
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] / 2
self.draw_label(xx, yy, label, "center")

# inside labels, top
if ii < self.num_flow - 1 and self.label_loc[1] in ("top"):
xx = x_lr[lr] + self.x_node_width / 2
for label in self.node_sizes[ii + lr]:
if (label not in self.node_sizes[ii]) or self.label_duplicate:
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] + self.y_label_gap
self.draw_label(xx, yy, label, "center")

# inside labels, right
if ii < self.num_flow - 1 and self.label_loc[1] in ("right", "both"):
xx = x_lr[lr] + self.x_label_gap + self.x_node_width
Expand All @@ -633,10 +645,13 @@ def subplot(self, ii):
xx = x_lr[lr] - self.x_label_gap
elif self.label_loc[2] in ("right"):
xx = x_lr[lr] + self.x_label_gap + self.x_node_width
elif self.label_loc[2] in ("center"):
elif self.label_loc[2] in ("center", "top"):
xx = x_lr[lr] + self.x_node_width / 2
for label in self.node_sizes[ii + lr]:
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] / 2
if self.label_loc[0] in ("top"):
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] + self.y_label_gap
else:
yy = self.node_pos_bot[ii][lr][label] + self.node_sizes[ii + lr][label] / 2
self.draw_label(xx, yy, label, ha_dict[self.label_loc[2]])

# Plot flows
Expand Down
21 changes: 12 additions & 9 deletions docs/example_plastics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,39 @@
])

plt.figure(dpi=600,figsize=(10,10))


sky.sankey(
data,
node_lw=3,
flow_lw=1.5,
label_width = 0.1,
label_gap = 0.05,
label_gap = 0.02,
node_width = 0.06,
node_gap = 0.05,
node_alpha = 0.5,
node_gap = 0.08,
node_alpha = 1,
node_edge = True,
flow_alpha = 0.4,
flow_edge = False,
flow_edge = True,
title_gap = 0.02,
title_side = "both",
title_loc = "outer",
frame_side = "both",
frame_lw = 0,
label_loc = ["left", "center", "right"],
label_loc = ["top", "top", "top"],
value_loc = ["right", "right", "right"],
sort = "top",
sort_dict = {"Still Used":999},
label_duplicate = False,
fontcolor = "blue",
label_duplicate = True,
# label_font = {"color": "red", "rotation": 90},
valign = "top",
value_gap = 0.03,
value_font = {"color": [0, 0, 0], "fontweight": "bold"}
# other_thresh_val = 3,
value_font = {"color": [0, 0, 0], "fontweight": "bold"},
label_font = {"color": [0, 0, 0], "fontweight": "bold"},
colormap = "jet",
)


plt.show()
plt.savefig("example_plastics.png")
plt.close()
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ The locations of the labels can be specified according to whether they correspon

label_loc = [ <loc_l> , <loc_m>, <loc_r> ]

Allowable values for `<loc_l>` and `<loc_r>` are `"left"`, `"right"`, `"center"`, or `"none"`.
Allowable values for `<loc_l>` and `<loc_r>` are `"left"`, `"right"`, `"center"`, `"top"`, or `"none"`.
`<loc_m>` also allows `"both"`. The default settings are:

label_loc = [ "left", "none", "right" ]
Expand Down

0 comments on commit d14becd

Please sign in to comment.