4

I have a data frame called final_net that resembles the source and the destination of tweets:

From     Destination    Count
A        B              31
A        C              25
B        C              24
...

What I am trying to do is to create a network diagram using Networkx. In order to do that, I have written the following code:

# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

plt.figure(figsize=(20,10))
# Build your graph
G=nx.from_pandas_edgelist(final_net.head(25), 'from', 'to', create_using=nx.DiGraph() )

# Custom the nodes:
nx.draw(G, with_labels=True, 
        node_color='skyblue', node_size=2200, 
        edge_color=final_net.head(25)['count'], width=3, edge_cmap=plt.cm.OrRd,
        arrowstyle='->',arrowsize=20,
        font_size=10, font_weight="bold",
        pos=nx.random_layout(G, seed=13))

I am taking only the most frequent 25 links and creating a network diagram. But I want to tidy the diagram a little bit more in such a way that:

  • no nodes overlap
  • no edges overlap
  • nodes are close to each other to avoid extra-long edges

My outcome is: enter image description here

realkes
  • 833
  • 1
  • 12
  • 20

0 Answers0