-1

I tried drawing sankey plot using python (hv and bokeh) but was not able to get a plot with the TARGETS and SOURCE sorted in the desired order. My data "sorted_df0" is :

            Source    Target   Value       Category_links
31         SQUALIDAE     DD      5         Data Deficient
35   PRISTIOPHORIDAE     DD      2         Data Deficient
20        SPHYRNIDAE     DD      1         Data Deficient
78   PSEUDOTRIAKIDAE     DD      1         Data Deficient
102     ETMOPTERIDAE     DD      1         Data Deficient
..               ...    ...    ...                    ...
27         TRIAKIDAE     CE      5  Critically Endangered
39       SQUATINIDAE     CE      2  Critically Endangered
87    CENTROPHORIDAE     CE      1  Critically Endangered
72      MYLIOBATIDAE     CE      1  Critically Endangered
14    CARCHARHINIDAE     CE      4  Critically Endangered

Code is as below...

sankey2 = hv.Sankey(sorted_df0,kdims = ["Source", "Target"], vdims = ["Value"])
color_palette = cc.glasbey_dark[:len(sorted_df0)]

#Define options for the Sankey diagram
opts.defaults(opts.Sankey(edge_color=hv.Cycle(values=color_palette)))
plt.tight_layout()
sankey2.opts(cmap = "PuBuGn_r", label_position='outer',width=1000, height=700,title = "AAAA")

Resulting Sankey diagram But I need image with Targets in the order as given in the data frame that is DD, LC, NT, VU, EN, CE rather than the order in the plot

Redox
  • 9,321
  • 5
  • 9
  • 26
Sajna v.h
  • 1
  • 1

1 Answers1

0

Sorting the data alone will not make holoviews sort the data. You need to use pandas.Categorical() to first assign the sorting order and then sort the columns. Add these two lines before the line sankey2 = hv.Sankey(...) line. Note that the odering is in reverse of what you need. Note that I was not able to get the plot to work exactly as was in your plot... no data and perhaps different versions, but this sorting should work for you.

sorted_df0['Target'] = pd.Categorical(sorted_df0['Target'], ["CE", "EN", "VU", "NT", "LC", "DD"])
sorted_df0.sort_values(['Target'], inplace=True)

Plot will look something like this

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • Can i get different coloured lines for each group? – Sajna v.h Jun 19 '23 at 07:28
  • Do you mean edge color? In the `sankey2.opts()` line, add `edge_color=cc.glasbey_dark` and it will take that palette. If you need a single color, use something like `edge_color="red"` – Redox Jun 19 '23 at 15:28