Monday, 8 July 2024

Top 3 Python Tools for Stunning Network Graphs

 



Top 3 Python Tools for Stunning Network Graphs

1. NetworkX

NetworkX is a powerful library for the creation, manipulation, and study of complex networks. It provides basic visualization capabilities, which can be extended using Matplotlib.


import networkx as nx

import matplotlib.pyplot as plt


# Create a graph

G = nx.erdos_renyi_graph(30, 0.05)


# Draw the graph

nx.draw(G, with_labels=True, node_color='skyblue', node_size=30, edge_color='gray')

plt.show()

No description has been provided for this image

2. Pyvis

Pyvis is a library built on top of NetworkX that allows for interactive network visualization in web browsers. It uses the Vis.js library to create dynamic and interactive graphs.


from pyvis.network import Network


import networkx as nx


# Create a graph

G = nx.erdos_renyi_graph(30, 0.05)


# Initialize Pyvis network

net = Network(notebook=True)


# Populate the network with nodes and edges from NetworkX graph

net.from_nx(G)


# Show the network

net.show("network.html")


#clcoding.com

Warning: When  cdn_resources is 'local' jupyter notebook has issues displaying graphics on chrome/safari. Use cdn_resources='in_line' or cdn_resources='remote' if you have issues viewing graphics in a notebook.

network.html


3. Plotly

Plotly is a graphing library that makes interactive, publication-quality graphs online. It supports interactive network graph visualization


import plotly.graph_objects as go

import networkx as nx


# Create a graph

G = nx.random_geometric_graph(200, 0.125)


# Extract the positions of nodes

pos = nx.get_node_attributes(G, 'pos')


# Create the edges

edge_x = []

edge_y = []

for edge in G.edges():

    x0, y0 = pos[edge[0]]

    x1, y1 = pos[edge[1]]

    edge_x.extend([x0, x1, None])

    edge_y.extend([y0, y1, None])


edge_trace = go.Scatter(

    x=edge_x, y=edge_y,

    line=dict(width=0.5, color='#888'),

    hoverinfo='none',

    mode='lines')


# Create the nodes

node_x = []

node_y = []

for node in G.nodes():

    x, y = pos[node]

    node_x.append(x)

    node_y.append(y)


node_trace = go.Scatter(

    x=node_x, y=node_y,

    mode='markers',

    hoverinfo='text',

    marker=dict(

        showscale=True,

        colorscale='YlGnBu',

        size=10,

        colorbar=dict(

            thickness=15,

            title='Node Connections',

            xanchor='left',

            titleside='right'

        )))


# Combine the traces

fig = go.Figure(data=[edge_trace, node_trace],

                layout=go.Layout(

                    title='Network graph made with Python',

                    showlegend=False,

                    hovermode='closest',

                    margin=dict(b=20, l=5, r=5, t=40),

                    xaxis=dict(showgrid=False, zeroline=False),

                    yaxis=dict(showgrid=False, zeroline=False)))


# Show the plot

fig.show()

 

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (203) Cybersecurity (24) data management (11) Data Science (107) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (25) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (50) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (897) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses