How can one modify the outline color of a node in networkx using Matplotlib?

MatplotlibPythonData Visualization

To modify the outline color of a node in networkx, we can use set_edgecolor() method.

Steps

  • Create a Pandas dataframe with from and to keys.
  • Return a graph from Pandas DataFrame containing an edge list.
  • Get the position of the nodes.
  • Draw the nodes of the graph using draw_networkx_nodes().
  • Set the outline color of the nodes using set_edgecolor().
  • To display the figure, use show() method.

Example

from networkx import *
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'from': ['A', 'B', 'C', 'A'], 'to': ['D', 'A', 'E', 'C']})
G = nx.from_pandas_edgelist(df, 'from', 'to')
pos = spring_layout(G)
nodes = draw_networkx_nodes(G, pos)
nodes.set_edgecolor('red')
plt.show()

Output

raja
Updated on 15-May-2021 12:30:46

Advertisements