SoFunction
Updated on 2024-11-15

An Example Exploration of the python gravis library for graphical data visualization

python gravis library

Today I'd like to share with you an amazing python library thatgravis

gravis (Graph Visualization Software) is a Python library for graphical data visualization. It focuses on providing a simple, efficient, and customizable way to present and explore graphical data.

It uses Python to prepare graphical data and Web technologies (HTML/CSS/JS) to render graphical data, mainly based on the JavaScript libraries , and / . The results can be displayed in a Web browser window, embedded in Jupyter Notebook, exported to a standalone HTML file, or used as HTML text in a Web application. Export of still images in JPG, PNG and SVG formats is also supported.

Some key features of the gravis library

  • usabilityIt provides a clean API to create and manipulate graphical visualizations.

  • Compatible with other graphics librariesgravis can be used in conjunction with other popular graphics processing libraries such as NetworkX, igraph, etc. This means that you can use these libraries to create and manipulate graphics and then use them for visualization. This means that you can use these libraries to create and manipulate graphics, and then use gravis to visualize them.

  • interactivityThe Gravis generatedGraphical visualizations are often interactives, which allows users to better explore the graphic by zooming, panning, hovering, and other operations.

  • Customizability, provides many customization options, such as the style of nodes and edges, the choice of layout algorithms, etc., so that users can adjust the appearance and behavior of the visualization according to their needs.

  • Multiple output formatsGravis supports a variety of output formats, including displaying them directly in Jupyter Notebook or exporting them as HTML files, images, and so on.

Library Installation

You can install it directly via pip.

pip install gravis

Graphics Generator

Here, we use nx.dual_barabasi_albert_graph to generate a random graph containing 30 nodes, and then add some attributes to the nodes and edges respectively.

import uuid
import networkx as nx
def get_new_test_graph():
    NUM_NODES = 30
    p = 0.5
    seed = 1
    test_graph = nx.dual_barabasi_albert_graph(n=NUM_NODES, p=p, seed=seed, m1=2, m2=1)

    # add node properties
    nx.set_node_attributes(test_graph, dict(test_graph.degree()), name='degree')
    nx.set_node_attributes(test_graph, nx.betweenness_centrality(test_graph), name='betweenness_centrality')

    for node, data in test_graph.nodes(data=True):
        data['node_identifier'] = str(uuid.uuid4())
        data['feature1'] = ()
        data['feature2'] = (0, high=100)
        data['feature3'] = 1 if () > 0.5 else 0

    # add edge properties
    for _, _, data in test_graph.edges(data=True):
        data['feature1'] = ()
        data['feature2'] = (0, high=100)
    
    return test_graph

When we plot the chart using networkx, we get the following result.

test_graph = get_new_test_graph()
(test_graph)

Drawing diagrams with gravis

import gravis as gv 
gv.d3(
    test_graph, 
    # graph specs
    graph_height=500,
    # node specs
    node_size_data_source="betweenness_centrality",
    use_node_size_normalization=True,
    node_size_normalization_min=15,
    node_size_normalization_max=35,
    show_node_label=True,
    node_label_data_source='node_identifier',
    # edge specs
    edge_size_data_source='feature1',
    use_edge_size_normalization=True,
    edge_size_normalization_min=1,
    edge_size_normalization_max=5,
    # force-directed graph specs
    many_body_force_strength=-500
)

Above is the python gravis library to achieve graphical data visualization examples to explore the details, more information about python gravis library please pay attention to my other related articles!