Holoviews Chord Diagram: How to get Edges & Nodes to have the same color?

2.3k Views Asked by At

I tried the holoviews example for creating a chord diagram here which works fine. However when trying to modify the chord diagram, I have never been able to to get the edges to have the same color as the nodes, color-grouped by their respective group.

The modified code I use:

    import pandas as pd
    import holoviews as hv
    from holoviews import opts, dim
    from bokeh.sampledata.les_mis import data
    
    hv.extension('bokeh')
    hv.output(size=200)
    
    links = pd.DataFrame(data['links'])
    nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index')
    
    chord = hv.Chord((links, nodes)).select(value=(5, None))
    chord.opts(opts.Chord(labels='name', 
                          cmap='Category20', 
                          edge_cmap='Category20', 
                          edge_color=dim('group').str(), 
                          node_color=dim('group').str()))

The Data:

    print(links.head(3))

links

    nodes.data.head()

nodes

The Result:

The nodes are colored correctly, by group, however the edges are all colored black.

Expected Result: The edges should have same color as the nodes.

A similar question for a different problem has been asked here, but no answers/solutions yet.

Any helps / suggestions are appreciated!

3

There are 3 best solutions below

3
On

The edge_color and node_color parameters should not be the same. This should work:

chord.opts(opts.Chord(labels='name',
                      cmap='Category20',
                      edge_color=dim('source').str(),
                      node_color=dim('name').str()))

I think node_color=dim('name').str())) could also be node_color=dim('index').str())).

0
On

For those who wanders here later, make sure your edge column is of type int64, and it will work

0
On

As I wrote in my comment above, one problem with the Holoviews example is that the original dataset is a dictionary that is then converted into a dataframe although some of the variables used still refer to the keys of the dictionary.

I have managed to adjust the process for data that come in dataframe format right from the start. From a larger dataframe with over ten columns, I select the columns I want to use as source (event_type) and target (place_name), and I calculate the weights of these connections. The top 10 most frequent connections in my dataset are written to a new dataframe for plotting the chord diagram. Accordingly, I use the column names from my top_10_df when defining the layout options for the chord diagram:

# Create a chord diagram with holoviews

import holoviews as hv
from holoviews import opts, dim

# Load the bokeh extension for holoviews
hv.extension('bokeh')

# Initialise diagram and define size
hv.output(size=150)

# Prepare "connections" file from original dataframe to meet requirements for chord diagrams
# Group input df by 'event_type' and 'place_name' and count occurrences
result_df = input_df.groupby(['event_type', 'place_name']).size().reset_index(name='weights') 

# Sort "weights" in descending order and select top 10 combinations
top_10_df = result_df.sort_values(by='weights', ascending=False).head(10)

display(top_10_df)

# Create a Holoviews Dataset for nodes
nodes = hv.Dataset(top_10_df, 'index')
nodes.data.head()

# Set parametres for the chord diagram and plot
chord = hv.Chord(top_10_df, vdims = "weights")
chord.opts(cmap = 'Category20',  # categorical color scheme
          edge_cmap = 'Category20',  # categorical color scheme
          labels = 'index',  # labels based on the index
          node_color = hv.dim('index').str(), # node colour based on index
          edge_color = hv.dim('event_type').str(), # edge colour based on the source, here: event_type)
          width = 300,
          height = 300)

The top_10_df, from which the diagram is plotted, looks like this:

event_type  place_name  weights
130     Funktionsausübung   Wetzlar     67
291     Präsentation    Wetzlar     64
2   Aufschwörung    Wetzlar     53
418     erfolglose Bewerbung    Wetzlar     53
256     Praktikum   Wetzlar     49
97  Funktionsausübung   Koblenz     45
404     Vokation    Wetzlar     43
103     Funktionsausübung   Mainz   40
107     Funktionsausübung   München     31
389     Tod     Wetzlar     31

The diagram I get is the following:

enter image description here