Export

paths.csv and map.png.

Write paths.csv and map.png.

Goal

Print the CSV and save the figure.

import csv
from pathlib import Path
import networkx as nx
G = nx.Graph()
G.add_edge('Nairobi', 'Nakuru', weight=160)
G.add_edge('Nakuru', 'Kisumu', weight=180)
path = nx.dijkstra_path(G, 'Nairobi', 'Kisumu')
Path('paths.csv').write_text('city\n' + '\n'.join(path) + '\n', encoding='utf-8')
print(Path('paths.csv').read_text(encoding='utf-8'))
import networkx as nx
G = nx.Graph([('Nairobi', 'Nakuru'), ('Nakuru', 'Kisumu')])
pos = {'Nairobi': (0, 0), 'Nakuru': (1, 1), 'Kisumu': (2, 0)}
nx.draw(G, pos, with_labels=True)
plt.savefig('map.png', dpi=120, bbox_inches='tight')
plt.show()
print('wrote map.png')