Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / advanced / plot_heavy_metal_umlaut.py @ 5cef0f13

History | View | Annotate | Download (1.94 KB)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""
4
==================
5
Heavy Metal Umlaut
6
==================
7

8
Example using unicode strings as graph labels.
9

10
Also shows creative use of the Heavy Metal Umlaut:
11
https://en.wikipedia.org/wiki/Heavy_metal_umlaut
12
"""
13
# Author: Aric Hagberg (hagberg@lanl.gov)
14

    
15
#    Copyright (C) 2006-2019 by
16
#    Aric Hagberg <hagberg@lanl.gov>
17
#    Dan Schult <dschult@colgate.edu>
18
#    Pieter Swart <swart@lanl.gov>
19
#    All rights reserved.
20
#    BSD license.
21

    
22
import matplotlib.pyplot as plt
23
import networkx as nx
24

    
25
try:
26
    hd = 'H' + unichr(252) + 'sker D' + unichr(252)
27
    mh = 'Mot' + unichr(246) + 'rhead'
28
    mc = 'M' + unichr(246) + 'tley Cr' + unichr(252) + 'e'
29
    st = 'Sp' + unichr(305) + 'n' + unichr(776) + 'al Tap'
30
    q = 'Queensr' + unichr(255) + 'che'
31
    boc = 'Blue ' + unichr(214) + 'yster Cult'
32
    dt = 'Deatht' + unichr(246) + 'ngue'
33
except NameError:
34
    hd = 'H' + chr(252) + 'sker D' + chr(252)
35
    mh = 'Mot' + chr(246) + 'rhead'
36
    mc = 'M' + chr(246) + 'tley Cr' + chr(252) + 'e'
37
    st = 'Sp' + chr(305) + 'n' + chr(776) + 'al Tap'
38
    q = 'Queensr' + chr(255) + 'che'
39
    boc = 'Blue ' + chr(214) + 'yster Cult'
40
    dt = 'Deatht' + chr(246) + 'ngue'
41

    
42
G = nx.Graph()
43
G.add_edge(hd, mh)
44
G.add_edge(mc, st)
45
G.add_edge(boc, mc)
46
G.add_edge(boc, dt)
47
G.add_edge(st, dt)
48
G.add_edge(q, st)
49
G.add_edge(dt, mh)
50
G.add_edge(st, mh)
51

    
52
# write in UTF-8 encoding
53
fh = open('edgelist.utf-8', 'wb')
54
fh.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))  # encoding hint for emacs
55
nx.write_multiline_adjlist(G, fh, delimiter='\t', encoding='utf-8')
56

    
57
# read and store in UTF-8
58
fh = open('edgelist.utf-8', 'rb')
59
H = nx.read_multiline_adjlist(fh, delimiter='\t', encoding='utf-8')
60

    
61
for n in G.nodes():
62
    if n not in H:
63
        print(False)
64

    
65
print(list(G.nodes()))
66

    
67
pos = nx.spring_layout(G)
68
nx.draw(G, pos, font_size=16, with_labels=False)
69
for p in pos:  # raise text positions
70
    pos[p][1] += 0.07
71
nx.draw_networkx_labels(G, pos)
72
plt.show()