Skip to content

Commit

Permalink
rename sorting to sort and take string input
Browse files Browse the repository at this point in the history
  • Loading branch information
wspr committed Mar 19, 2024
1 parent c84d7f0 commit 01036a4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
27 changes: 21 additions & 6 deletions ausankey/ausankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sankey(
title_gap=0.05,
title_side="top", # "bottom", "both"
title_loc="inner", # "outer"
sorting=0,
sort="bottom", # "top", "bottom", "none"
valign="bottom", # "top","center"
):
"""Make Sankey Diagram with left-right flow
Expand Down Expand Up @@ -110,7 +110,7 @@ def sankey(
plot edges and the label
(1.0 = 100% of plot width)
sorting : int
sort : int
Parity bit for how to sort the display of the data.
`0` is unsorted — display data in order it appears in the DataFrame.
`1` and `-1` sort high to low or vice versa.
Expand Down Expand Up @@ -176,7 +176,6 @@ def sankey(
weight_stop = data[2 * ii + 1][ind_this & ind_prev & ~ind_next].sum()
weight_strt = data[2 * ii + 1][ind_this & ~ind_prev & ind_next].sum()

weight_next = data[2 * ii + 1][ind_next].sum()
weight_sum[ii] = weight_cont + weight_only + max(weight_stop, weight_strt)

for ii in range(num_side):
Expand Down Expand Up @@ -263,7 +262,7 @@ def sankey(
plot_height=plot_height,
alpha=alpha,
voffset=voffset,
sorting=sorting,
sorting=sort,
ax=ax,
)

Expand Down Expand Up @@ -527,17 +526,33 @@ def draw_bar(x, dx, y, dy, label):
def weighted_sort(lbl, wgt, sorting):
"""creates a sorted list of labels by their summed weights"""

if sorting == "top":
s = 1
elif sorting == "bottom":
s = -1
elif sorting == "center":
s = 1
else:
s = o

Check failure on line 536 in ausankey/ausankey.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F821)

ausankey/ausankey.py:536:13: F821 Undefined name `o`

arr = {}
for uniq in lbl.unique():
arr[uniq] = wgt[lbl == uniq].sum()

sort_arr = sorted(
arr.items(),
key=lambda item: sorting * item[1],
key=lambda item: s * item[1],
# sorting = 0,1,-1 affects this
)

return list(dict(sort_arr))
sorted_labels = list(dict(sort_arr))

if sorting == "center":
# this kinda works but i dont think it's a good idea because you lose perception of relative sizes
# probably has an off-by-one even/odd error
sorted_labels = sorted_labels[1::2] + sorted_labels[-1::-2]

return sorted_labels


###########################################
Expand Down
15 changes: 10 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ The order of the entries is top to bottom corresponding to first to last in the

The entries can be sorted highest to lowest:
```
sky.sankey(data,sorting=1)
sky.sankey(data,sort=“top”)
```
![Image with options](fruits_sort_p1.png)
![Image with options](fruits_sort_top.png)

Or lowest to highest:
```
sky.sankey(data,sorting=-1)
sky.sankey(data,sorting=“bottom”)
```
![Image with options](fruits_sort_n1.png)
![Image with options](fruits_sort_bot.png)

Or left in the order listed in the data:
```
sky.sankey(data,sorting=“none”)
```
![Image with options](fruits_sort_none.png)

## Labels

Expand Down Expand Up @@ -182,7 +187,7 @@ sky.sankey( data,
titles = [“Summer”,”Winter”],
title_side = “both”,
frame_side = “both”,
sorting = -1,
sorting = “top”,
valign = “center”,
# spacing parameters:
bar_gap = 0.01 ,
Expand Down
15 changes: 10 additions & 5 deletions docs/sankey_doc_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@
plt.savefig("fruits_alpha.png")

plt.figure()
sky.sankey(data, sorting=1)
sky.sankey(data, sort="top")
plt.show()
plt.savefig("fruits_sort_p1.png")
plt.savefig("fruits_sort_top.png")

plt.figure()
sky.sankey(data, sorting=-1)
sky.sankey(data, sort="bottom")
plt.show()
plt.savefig("fruits_sort_n1.png")
plt.savefig("fruits_sort_bot.png")

plt.figure()
sky.sankey(data, sort="none")
plt.show()
plt.savefig("fruits_sort_none.png")

plt.figure()
sky.sankey(data, titles=["Summer", "Winter"])
Expand Down Expand Up @@ -101,7 +106,7 @@
titles=["Summer", "Winter"],
title_side="both",
frame_side="both",
sorting=-1,
sort="top",
valign="center",
# spacing parameters:
bar_gap=0.01,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_fruit_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ def test_fruits_default(self):

def test_fruits_sorting(self):
plt.figure(dpi=150)
sky.sankey(self.data, sorting=1)
sky.sankey(self.data, sort="bottom")

plt.figure(dpi=150)
sky.sankey(self.data, sorting=-1)
sky.sankey(self.data, sort="top")

plt.figure(dpi=150)
sky.sankey(self.data, sort="none")

def test_fruits_colormap(self):
plt.figure(dpi=150)
Expand Down

0 comments on commit 01036a4

Please sign in to comment.