1
$\begingroup$

I have a large data set similar to that in the screenshot below:

enter image description here

and I want to visualise the whole data set like the diagram below (made with a lot of effort in PowerPoint!)

enter image description here

Is there any way to do this for the whole data set in preferably Python but if there is a way in Excel, that would also be great.

Any help would be much appreciated!

Thanks.

$\endgroup$

1 Answer 1

1
$\begingroup$

Here's a solution in Python. It uses pandas to load the data from an excel file, NetworkX to create a network from that data, and gravis to visualize the network. Disclaimer: I'm the author of gravis, an open source Python package for network visualization.

import gravis as gv
import networkx as nx
import pandas as pd

df = pd.read_excel('data.xlsx')

x_scale = 100
y_scale = 60
colors = ['blue', 'orange', 'gray', '#ffcc55', 'lightblue', 'green', '#dddd00']

dg = nx.DiGraph()
entry_to_color = dict()
entry_counter = 0
dg.graph['node_shape'] = 'rectangle'
for i, col_name in enumerate(df.columns):
    col = df[col_name]
    for j, entry in enumerate(col):
        node = str(i) + str(entry)
        if entry not in entry_to_color:
            entry_to_color[entry] = colors[entry_counter]
            entry_counter += 1
        dg.add_node(node, name=entry, x=i*x_scale, y=j*y_scale, i=i)

for i, n_name in enumerate(dg.nodes):
    node = dg.nodes[n_name]
    color = entry_to_color[node['name']]
    node['color'] = color
    node['label_color'] = color

for n1_name in dg.nodes:
    for n2_name in dg.nodes:
        n1 = dg.nodes[n1_name]
        n2 = dg.nodes[n2_name]
        if n1 != n2 and n1['name'] == n2['name'] and n2['i'] == n1['i']+1:
            dg.add_edge(n1_name, n2_name, color=n1['color'])

gv.d3(dg, node_label_data_source='name')

Here's the output if you run that code inside a Jupyter notebook, although it could also be done in the normal Python interpreter:

network

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.