Created
February 4, 2012 21:02
-
-
Save johnjosephhorton/1740131 to your computer and use it in GitHub Desktop.
Stereotypes about animals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
relationships = { | |
'cats':['cute', 'clean', 'curious', 'lazy'], | |
'children':['cruel', 'happy', 'mean', 'stupid'], | |
'cows':['fat', 'sacred to hindus', 'stupid', 'sacred'], | |
'dogs':['loyal', 'cute', 'loving', 'happy'], | |
'frogs':['happy', 'slimy', 'important', 'sensitive to pollution'], | |
'goldfish':['dirty', 'good', 'addicting', 'hard to keep alive'], | |
'hamsters':['cute', 'stupid', 'little'], | |
'mice':['cute', 'scary', 'smart', 'small'], | |
'turtles':['slow', 'awesome', 'cool', 'important'], | |
'pigs':['smart', 'fat', 'similar to humans', 'salty'], | |
} | |
G = nx.Graph() | |
G.type = {} | |
# add entities | |
for r in relationships: | |
G.add_node(r) | |
G.type[r] = "Entity" | |
# add attributes | |
_attributes = [] | |
for attr in relationships.values(): | |
_attributes.extend(attr) | |
attributes = list(set(_attributes)) | |
for a in attributes: | |
G.add_node(a) | |
G.type[a] = "Attribute" | |
# add all the edges | |
for key, attrs in relationships.items(): | |
for a in attrs: | |
G.add_edge(key, a) | |
node_color = [] | |
node_size = [] | |
for n in G.nodes(): | |
node_color.append(.1 if G.type[n] == 'Entity' else .8) | |
node_size.append(600 if G.type[n] == 'Entity' else 100) | |
pos = nx.graphviz_layout(G) | |
nx.draw(G, | |
pos, | |
node_size=node_size, | |
node_color = node_color, | |
edge_color='black', | |
font_size=8, | |
alpha = .2) | |
plt.savefig("relationships.png",dpi=200) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment