Open essepuntato opened 4 years ago
from networkx import MultiDiGraph, draw
import matplotlib.pyplot as plt
movies_characters_graph = MultiDiGraph()
# Actors
movies_characters_graph.add_node("Brad Pitt")
movies_characters_graph.add_node("Eva Green")
movies_characters_graph.add_node("George Clooney")
movies_characters_graph.add_node("Catherine Zeta-Jones")
movies_characters_graph.add_node("Johnny Depp")
movies_characters_graph.add_node("Helena Bonham Carter")
# Cast of Ocean's Twelve
movies_characters_graph.add_edge("Brad Pitt", "Ocean's Twelve")
movies_characters_graph.add_edge("Catherine Zeta-Jones", "Ocean's Twelve")
movies_characters_graph.add_edge("George Clooney", "Ocean's Twelve")
# Cast of Fight Club
movies_characters_graph.add_edge("Brad Pitt", "Fight Club")
movies_characters_graph.add_edge("Helena Bonham Carter", "Fight Club")
# Cast of Dark Shadows
movies_characters_graph.add_edge("Johnny Depp", "Dark Shadows")
movies_characters_graph.add_edge("Helena Bonham Carter", "Dark Shadows")
movies_characters_graph.add_edge("Eva Green", "Dark Shadows")
print(movies_characters_graph.nodes())
# ['Brad Pitt', 'Eva Green', 'George Clooney', 'Catherine Zeta-Jones', 'Johnny Depp', 'Helena Bonham Carter', "Ocean's Twelve", 'Fight Club', 'Dark Shadows']
print(movies_characters_graph.edges())
# [('Brad Pitt', "Ocean's Twelve"), ('Brad Pitt', 'Fight Club'), ('George Clooney', "Ocean's Twelve"), ('Catherine Zeta-Jones', "Ocean's Twelve"),
# ('Johnny Depp', 'Dark Shadows'), ('Helena Bonham Carter', 'Fight Club'), ('Helena Bonham Carter', 'Dark Shadows')]
draw(movies_characters_graph, with_labels = True)
plt.savefig("filename.png")
from networkx import MultiDiGraph
movies_graph = MultiDiGraph()
movies_graph.add_node("Brad Pitt")
movies_graph.add_node("Eva Green")
movies_graph.add_node("George Clooney")
movies_graph.add_node("Catherine Zeta-Jones")
movies_graph.add_node("Johnny Depp")
movies_graph.add_node("Helena Bonham Carter")
movies_graph.add_node("Ocean's Twelve")
movies_graph.add_node("Fight Club")
movies_graph.add_node("Dark Shadows")
movies_graph.add_edge("Brad Pitt", "Fight Club")
movies_graph.add_edge("Brad Pitt", "Ocean's Twelve")
movies_graph.add_edge("Eva Green", "Dark Shadows")
movies_graph.add_edge("George Clooney", "Ocean's Twelve")
movies_graph.add_edge("Catherine Zeta-Jones", "Ocean's Twelve")
movies_graph.add_edge("Johnny Depp", "Dark Shadows")
movies_graph.add_edge("Helena Bonham Carter", "Fight Club")
movies_graph.add_edge("Helena Bonham Carter", "Dark Shadows")
print(movies_graph.nodes())
#['Brad Pitt', 'Eva Green', 'George Clooney', 'Catherine Zeta-Jones', 'Johnny Depp', 'Helena Bonham Carter', "Ocean's Twelve", 'Fight Club', 'Dark Shadows']
print(movies_graph.edges())
#[('Brad Pitt', 'Fight Club'), ('Brad Pitt', "Ocean's Twelve"), ('Eva Green', 'Dark Shadows'), ('George Clooney', "Ocean's Twelve"), ('Catherine Zeta-Jones', "Ocean's Twelve"), ('Johnny Depp', 'Dark Shadows'), ('Helena Bonham Carter', 'Fight Club'), ('Helena Bonham Carter', 'Dark Shadows')]
Used matplotlib as suggested by @arcangelo7 in order to visualize the graph.
Create a directed graph which relates the actors Brad Pitt, Eva Green, George Clooney, Catherine Zeta-Jones, Johnny Depp, and Helena Bonham Carter to the following movies: Ocean's Twelve, Fight Club, Dark Shadows.