import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
# Retina display
%config InlineBackend.figure_format = 'retina'
%pip install pydot
Requirement already satisfied: pydot in /home/nipun.batra/miniforge3/lib/python3.9/site-packages (1.4.2)
Requirement already satisfied: pyparsing>=2.1.4 in /home/nipun.batra/miniforge3/lib/python3.9/site-packages (from pydot) (3.0.7)
Note: you may need to restart the kernel to use updated packages.
from tueplots import bundles
plt.rcParams.update(bundles.icml2022())
# Also add despine to the bundle using rcParams
'axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams[
# Increase font size to match Beamer template
'font.size'] = 16
plt.rcParams[# Make background transparent
'figure.facecolor'] = 'none' plt.rcParams[
# Create a simple binary tree in networkx and plot it with dot layout
# +
= nx.Graph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos =True, node_size=1000, node_color='white') nx.draw(G, pos, with_labels
/home/nipun.batra/miniforge3/lib/python3.9/site-packages/IPython/core/pylabtools.py:151: UserWarning: There are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keyword
fig.canvas.print_figure(bytes_io, **kw)
= nx.Graph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos =True, node_size=500, node_color='white', node_shape='s', edgecolors='black', linewidths=2)
nx.draw(G, pos, with_labels# Show circle around root node
plt.tight_layout()
/tmp/ipykernel_2537678/3260582450.py:8: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
plt.tight_layout()
# Edges have string labels
= nx.DiGraph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2, {'label': '0'}), (1, 3, {'label': '1'}), (2, 4, {'label': '0'}), (2, 5, {'label': '1'}), (3, 6, {'label': '0'}), (3, 7, {'label': '1'})])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos =True, node_size=500, node_color='white', node_shape='s', edgecolors='black', linewidths=2)
nx.draw(G, pos, with_labels
# Print edge labels
= nx.get_edge_attributes(G, 'label')
labels = nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
_
# Colour 0 edges red and 1 edges blue
= nx.DiGraph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2, {'label': '0'}), (1, 3, {'label': '1'}), (2, 4, {'label': '0'}), (2, 5, {'label': '1'}), (3, 6, {'label': '0'}), (3, 7, {'label': '1'})])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos #nx.draw(G, pos, with_labels=True, node_size=500, node_color='white', node_shape='s', edgecolors='black', linewidths=2)
# Print edge labels
= nx.get_edge_attributes(G, 'label')
labels = ['red' if label == '0' else 'blue' for label in labels.values()]
edge_colors =True, node_size=500, node_color='white', node_shape='s', edgecolors='black', linewidths=2, edge_color=edge_colors)
nx.draw(G, pos, with_labels
= nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
_
# Colour odd nodes as black and even nodes as magenta with alpha=0.2
= nx.DiGraph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2, {'label': '0'}), (1, 3, {'label': '1'}), (2, 4, {'label': '0'}), (2, 5, {'label': '1'}), (3, 6, {'label': '0'}), (3, 7, {'label': '1'})])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos
# Print edge labels
= nx.get_edge_attributes(G, 'label')
labels = ['red' if label == '0' else 'blue' for label in labels.values()]
edge_colors = ['black' if node % 2 == 1 else 'magenta' for node in G.nodes]
node_colors
#nx.draw(G, pos, with_labels=True, node_size=500, node_color=node_colors, node_shape='s', edgecolors='black', linewidths=2, edge_color=edge_colors, alpha=0.6)
# Make edge lines dashed and weight 2
=True, node_size=500, node_color=node_colors, node_shape='s',
nx.draw(G, pos, with_labels='black', linewidths=2, edge_color=edge_colors, alpha=0.6, style='dashed', width=2)
edgecolors
# Make text labels white
= nx.draw_networkx_labels(G, pos, font_color='white') _
= nx.DiGraph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2, {'label': '0'}), (1, 3, {'label': '1'}), (2, 4, {'label': '0'}), (2, 5, {'label': '1'}), (3, 6, {'label': '0'}), (3, 7, {'label': '1'})])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos
# Print edge labels
= nx.get_edge_attributes(G, 'label')
labels = ['red' if label == '0' else 'blue' for label in labels.values()]
edge_colors = ['green' if node % 2 == 1 else 'magenta' for node in G.nodes]
node_colors
= plt.figure()
fig # Make edge lines dashed and weight 2
=True, node_size=500, node_color=node_colors, node_shape='s',
nx.draw(G, pos, with_labels='white', linewidths=2, edge_color=edge_colors, alpha=0.6, style='dashed', width=2)
edgecolors
"#00000F")
fig.set_facecolor(
# Make text labels white
= nx.draw_networkx_labels(G, pos, font_color='white')
_
= nx.DiGraph()
G 1, 2, 3, 4, 5, 6, 7])
G.add_nodes_from([1, 2, {'label': '0'}), (1, 3, {'label': '1'}), (2, 4, {'label': '0'}), (2, 5, {'label': '1'}), (3, 6, {'label': '0'}), (3, 7, {'label': '1'})])
G.add_edges_from([(
= nx.nx_agraph.graphviz_layout(G, prog='dot')
pos
# Print edge labels
= nx.get_edge_attributes(G, 'label')
labels = ['red' if label == '0' else 'blue' for label in labels.values()]
edge_colors = ['green' if node % 2 == 1 else 'magenta' for node in G.nodes]
node_colors
= plt.figure()
fig # Make edge lines dashed and weight 2
=True, node_size=500, node_color=node_colors, node_shape='s',
nx.draw(G, pos, with_labels='white', linewidths=2, edge_color=edge_colors, alpha=0.6, style='dashed', width=2)
edgecolors
"#000001")
fig.set_facecolor(
# Make text labels white
= nx.draw_networkx_labels(G, pos, font_color='white')
_
# Put coding for each node. Each red appends 0 and each blue appends 1
# codings[1] = '0'
# codings[2] = '00'
# codings[3] = '01'
# codings[4] = '000' and so on
= {}
codings 1] = '0'
codings[2] = '00'
codings[3] = '01'
codings[4] = '000'
codings[5] = '001'
codings[6] = '010'
codings[7] = '011'
codings[
# Put codings on the left of each node in white
for node, coding in codings.items():
0] - 40, pos[node][1], coding, color='white', fontsize=12) plt.text(pos[node][