Lots of edges on a graph plot in python

后端 未结 3 939
别跟我提以往
别跟我提以往 2020-12-16 15:42

I have following script:

import pandas as pd
from igraph import *

df_p_c = pd.read_csv(\'data/edges.csv\')

...

edges = list_edges
vertices = list(dict_cas         


        
3条回答
  •  甜味超标
    2020-12-16 16:17

    You could checkout networkx, which is a pretty nice graph library. Networkx has direct plotting support for matplotlib.

    It supports various layout types, for example spring layout, random layout, and a few more

    You should especially look at spring layout, which has a few interesting parameters for your use-case:

    k (float (default=None)) – Optimal distance between nodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes. Increase this value to move nodes farther apart.

    Or both of these in combination with a custom layout:

    pos (dict or None optional (default=None)) – Initial positions for nodes as a dictionary with node as keys and values as a coordinate list or tuple. If None, then use random initial positions.

    fixed (list or None optional (default=None)) – Nodes to keep fixed at initial position.

    The edge weight might also be something you can tune in order to get results you like:

    weight (string or None optional (default=’weight’)) – The edge attribute that holds the numerical value used for the edge weight. If None, then all edge weights are 1.

    I would recommend combining networkx with bokeh, which is a new plotting library that creates web-based html/js plots. It has direct support for networkx, and has some nice features like easy integration of node hover tools. If your graph isn't too big, the performance is pretty good. (I've plotted graphs with about 20000 nodes and a few thousand edges).

    With both libraries combined, all you need is the following bit of code for a simple example (from the documentation) that tries to build an optimized layout:

    import networkx as nx
    
    from bokeh.io import show, output_file
    from bokeh.plotting import figure
    from bokeh.models.graphs import from_networkx
    
    G=nx.karate_club_graph()  # Replace with your own graph
    
    plot = figure(title="Networkx Integration Demonstration", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
                  tools="", toolbar_location=None)
    
    graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))
    plot.renderers.append(graph)
    
    output_file("networkx_graph.html")
    show(plot)
    

提交回复
热议问题