Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

epaaso
Copy link

@epaaso epaaso commented Sep 1, 2025

Fixes Issue #3835.

To avoid messing with internal as much as possible, I changed the code such that to be able to use this fix with mpl you have to define the nodes object as a Dataset with the column for color as a vdim. Like in this snippet:

nodes_ds = Dataset(nodes_df, kdims=['label'], vdims=['color'])
sk = hv.Sankey((links_df, nodes_ds), kdims=['source', 'target'], vdims=['value', 'color'])

sk = sk.opts(
        title='Minimal Sankey (matplotlib)',
        labels='label',
        edge_color='color',      # constant or map; here field -> edges (length = edges)
        node_color='color',      # taken from node vdims (length = nodes)
        fig_inches=500/150,
        aspect=350/500,
        bgcolor='white'
    )

The bokeh parameter node_color is for a fixed color, so one has to use node_fill_color instead:

nodes_ds = Dataset(nodes_df, kdims=['label'], vdims=['color'])
sk = hv.Sankey((links_df, nodes_ds), kdims=['source', 'target'], vdims=['value', 'color'])
sk = sk.opts(
        title='Minimal Sankey (bokeh)',
        labels='label',
        edge_color='color',
        node_fill_color='color',
        width=500, height=350,
        bgcolor='white'
    )

There is an automatic assigning of cdim in both the pipeline for these backends and I do not prevent that. I just changed the code so that if node_color is defined, that parameter is given more importance. Maybe a more complete version of this PR would avoid auto-assigning cdim if node_color is given. But as cdim is deprecated, I decided to not waste much time looking for the auto-assign.

Copy link

codecov bot commented Sep 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.97%. Comparing base (2dfe271) to head (aeaead6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6678   +/-   ##
=======================================
  Coverage   88.97%   88.97%           
=======================================
  Files         328      328           
  Lines       70320    70324    +4     
=======================================
+ Hits        62570    62574    +4     
  Misses       7750     7750           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@holovizbot
Copy link

This pull request has been mentioned on HoloViz Discourse. There might be relevant details there:

https://discourse.holoviz.org/t/node-color-parameter-does-not-work-with-sankey/8245/4

@@ -653,6 +653,8 @@ def _apply_transforms(self, element, ranges, style):
elif type(element) is Path:
val = np.concatenate([v.apply(el, ranges=ranges, flat=True)
for el in element.split()])
elif 'node' in k:
val = v.apply(element.nodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still want to provide the ranges here (and in the bokeh case).

@@ -1838,6 +1838,8 @@ def _apply_transforms(self, element, data, ranges, style, group=None):
ds = Dataset({d.name: v for d, v in self.overlay_dims.items()},
list(self.overlay_dims))
val = v.apply(ds, ranges=ranges, flat=True)[0]
elif 'node' in k:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be implemented by implementing an overridden _element_transform method on GraphPlot. Annoyingly though we don't
pass k through to that method, so for now I guess this is the only thing we can do.

@philippjfr
Copy link
Member

Thanks @epaaso, really appreciate the contribution. A simple test in holoviews/tests/plotting/bokeh/test_graph.py would also be greatly appreciated.

@epaaso
Copy link
Author

epaaso commented Sep 6, 2025

Hey, of course give me 1-2 days!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants