NetworkX: Adding Attributes














































NetworkX: Adding Attributes



Python: NetworkX

NetworkX: Adding Attributes

Attributes such as weights, labels, colors or any other property can be attached to graphs, nodes or edges.

Each graph, node and edge can hold key-value pairs of attributes. By default these are empty. But attributes can be added or changed, using add_edge() and add_node() methods, or directly manipulated.

Graph attributes

Graph attributes can be assigned when creating a new graph.

import networkx as nx G = nx.Graph(day="Friday") print(G.graph)

The output looks like this

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red'}), (4, 5, {'color': 'red'})] [(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red', 'weight': 4.2}), (4, 5, {'color': 'red'})]

We can also modify attributes later.

import networkx as nx G = nx.Graph(day="Friday") print(G.graph) G.graph['day'] = 'Monday' print(G.graph)

The output looks like this

{'day': 'Friday'}

{'day': 'Monday'}

Node attributes

We can add node attributes using add_node(), add_nodes_from() or nodes.

import networkx as nx G = nx.Graph() G.add_node(1, time='5pm') G.add_nodes_from([3], time='2pm') print(G.nodes[1]) G.nodes[1]['room'] = 714 print(G.nodes.data())

The output looks like this

{'time': '5pm'} [(1, {'time': '5pm', 'room': 714}), (3, {'time': '2pm'})]

Edge attributes

Similar to node attributes, we can add edge attributes using add_edge(), add_edges_from() and edges.

import networkx as nx G = nx.Graph() G.add_edge(1, 2, weight=4.7) G.add_edges_from([(3, 4), (4, 5)], color='red') G.add_edges_from([(1, 2, {'color': 'blue'}), (2, 3, {'weight': 8})]) print(G.edges.data()) G[1][2]['weight'] = 4.7 G.edges[3, 4]['weight'] = 4.2 print(G.edges.data())

The output looks like this

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red'}), (4, 5, {'color': 'red'})]

[(1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {'weight': 8}), (3, 4, {'color': 'red', 'weight': 4.2}), (4, 5, {'color': 'red'})]


More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1026 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1672 2
nose: Working 507 2
pytest: Working 511 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 663 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1633 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 508 2
NetworkX: Multigraphs 648 2
Tracking User Activity with CherryPy 1397 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1301 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 483 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 607 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 612 2
Top 5 Free Online IDEs of 2020 866 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2279 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments