Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
6
Indexable
import networkx as nx
import matplotlib.pyplot as plt

# Example JSON-like data structure, assuming each node has an 'id' and 'attributes'
data = {
    "nodes": [
        {"id": "CardAvailable", "attributes": {"Available": True, "CardActive": True}},
        {"id": "CardRegistered", "attributes": {"Registered": True, "CardInternational": False}},
        {"id": "CardInternational", "attributes": {"CardInternational": False, "DoesCardHasValid": False}},
        {"id": "RegisteredStateEscheat", "attributes": {"RegisteredState": True}}
    ],
    "edges": [
        {"from": "CardAvailable", "to": "CardRegistered"},
        {"from": "CardRegistered", "to": "CardInternational"},
        {"from": "CardInternational", "to": "RegisteredStateEscheat"}
    ]
}

# Create a NetworkX directed graph
G = nx.DiGraph()

# Add nodes with attributes
for node in data["nodes"]:
    G.add_node(node["id"], **node["attributes"])

# Add edges between nodes
for edge in data["edges"]:
    G.add_edge(edge["from"], edge["to"])

# Draw the graph
pos = nx.spring_layout(G)  # positions for all nodes

# nodes
nx.draw_networkx_nodes(G, pos, node_size=700)

# edges
nx.draw_networkx_edges(G, pos, arrows=True)

# node labels
labels = {node['id']: node['id'] for node in data["nodes"]}
nx.draw_networkx_labels(G, pos, labels, font_size=12)

# edge labels
edge_labels = {(edge['from'], edge['to']): ' ' for edge in data["edges"]}  # Dummy space for edge labels
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

plt.axis('off')  # Turn off the axis
plt.show()  # Display the graph

Editor is loading...
Leave a Comment