nandz123
nandz123

Reputation: 169

Formatting data using Chord library in Python

I'm using chord to create a chord diagram that illustrates interaction amongst individuals.

I have a CSV file that has data as such:

name1 name2 interaction (1-5)
Ana   Sam      2
Sam   Chris    4
Chris Ana      1

I wasn't sure how to format the data in a matrix format that's accepted by the library. Any suggestions?

Upvotes: 1

Views: 152

Answers (1)

mouwsy
mouwsy

Reputation: 1933

Your data is in the right format. I am pretty sure that matrix format is not accepted by holoviews. Here is a minimal example with your data using jupyter notebook:

import pandas as pd
import holoviews as hv
hv.extension('bokeh')

df = pd.DataFrame([("Ana", "Sam", 2),
                   ("Sam", "Chris", 4),
                   ("Chris", "Ana", 1)],
                   columns=["name1", "name2", "interaction"])


nodes = list(set(df["name1"].unique().tolist() + df["name2"].unique().tolist()))
nodes_dataset = hv.Dataset(pd.DataFrame(nodes, columns=["all_names"]))

chord = hv.Chord((df, nodes_dataset)).opts(labels="all_names")
chord

For more details, see: https://coderzcolumn.com/tutorials/data-science/how-to-plot-chord-diagram-in-python-holoviews#Chord-Diagram-Showing-Traffic-Movement-Between-Cities-

Upvotes: 1

Related Questions