Search code examples
pythongraphhashgraphhedera-hashgraph

Plotting a Hashgraph Simulation in Python


I've been working on a Hashgraph simulation for some time and I'm having trouble trying to figure out how to plot data in a hashgraph data structure.

Hashgraph visual representation

My hashgraph data structure is a dictionary containing keys (which would be the names of each member) that correspond to lists containing event objects (which are represented by circles in the graphic above) for each respective member. Events are added randomly and have two edges: one pointing to the self-parent member's most recent Event, and another pointing towards the last event created by the member initiating the sync.

Sorry if that didn't make a lot of sense! I can explain more if anyone would like, but I figured that's all the explaining that needs to be done for the help I need. I'm not entirely sure what approach or library to use to create this kind of graph, but any help would be much appreciated!


Solution

  • Have a look at , it's great!

    By way of example; adapted from this:

    import matplotlib.pyplot as plt
    import networkx as nx
    
    G = nx.house_graph()
    # explicitly set positions
    pos = {0: (0.5, 0.5),
           1: (1, 0.5),
           2: (0.5, 1),
           3: (1, 1),
           4: (1, 2)}
    
    
    nx.draw_networkx_nodes(G, pos, node_size=3000, nodelist=[0, 1, 2, 3,4], node_color='b')
    nx.draw_networkx_edges(G, pos, alpha=0.5, width=6)
    plt.axis('off')
    plt.tight_layout()
    plt.show()
    

    enter image description here