Skip to content

Instantly share code, notes, and snippets.

@eightants
Created November 25, 2020 05:34
Show Gist options
  • Save eightants/fa3d81f8a50efd2c072c1b012691522f to your computer and use it in GitHub Desktop.
Save eightants/fa3d81f8a50efd2c072c1b012691522f to your computer and use it in GitHub Desktop.
Modified gantt plot to visualize the appearances of characters in the manga 'Kanojo, Okarishimasu'
# Given a one-hot encoded csv file with character names as the columns and each row representing a chapter/episode,
# this script generates a gantt chart timeline for the appearances of each character.
# Created by Anthony Teo
# Example: mainapps.csv
# Kazuya,Ruka,Mami
# 1,0,0
# 0,1,1
# 1,1,1
# 1,0,0
import plotly.figure_factory as ff
import plotly.express as px
from datetime import datetime
import numpy as np
import sys
import os
import pandas as pd
def convert_to_datetime(x):
return datetime.fromtimestamp(31536000+x*24*3600).strftime("%Y-%m-%d")
data = pd.read_csv(os.path.join(sys.path[0], 'mainapps.csv'))
chapters = 0
df = []
for col in data.columns:
start = -1
if chapters != len(data[col]):
chapters = len(data[col])
for i in range(len(data[col])):
val = data[col][i]
if val == 1:
if start == -1:
start = i
else:
if start != -1:
df.append(dict(Task="Appearance", Start=convert_to_datetime(
start), Finish=convert_to_datetime(i), Character=col))
start = -1
if start != -1:
df.append(dict(Task="Appearance", Start=convert_to_datetime(
start), Finish=convert_to_datetime(i + 1), Character=col))
start = -1
num_tick_labels = np.linspace(
start=0, stop=chapters, num=chapters + 1, dtype=int)
date_ticks = [convert_to_datetime(x) for x in num_tick_labels]
fig = px.timeline(df, x_start="Start", x_end="Finish",
y="Character", color="Character")
fig.update_layout({
"plot_bgcolor": 'rgba(0,0,0,0.0)',
"paper_bgcolor": 'rgba(0,0,0,0.0)'
})
fig.layout.xaxis.update({
'tickvals': date_ticks,
'ticktext': num_tick_labels
})
fig.show()
Kazuya Kinoshita Chizuru Ichinose Ruka Sarashina Mami Nanami Sumi Sakurasawa Mini Yaemori
1 1 0 1 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 1 0 0
1 1 0 1 0 0
1 1 0 1 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1 0 1 0 1 0
1 1 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 1 0 0 1 0
0 1 0 0 0 0
0 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 1
1 1 0 0 0 1
1 1 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 0 0 0 0
1 1 1 0 0 1
1 0 0 0 0 1
1 1 0 0 0 1
1 1 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 1
1 1 1 0 0 1
1 1 0 0 1 1
1 1 0 0 0 1
1 1 0 0 0 1
1 1 0 0 0 1
1 1 0 0 0 1
1 1 1 0 0 1
1 1 1 0 0 1
1 1 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 0 0 0 0
1 1 0 0 0 1
1 1 1 0 0 1
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 0 1 0 0 0
1 0 1 0 0 0
1 0 1 0 0 0
1 0 1 0 0 0
1 1 0 0 1 0
1 1 1 0 0 1
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 1
1 1 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 0 0 1 0
1 0 1 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
# This script scrapes the 'Characters' field in the KanoKari wiki and exports it as a one-hot encoded csv file.
import requests
from bs4 import BeautifulSoup
import pandas as pd
chapters = 165
charApps = []
charMap = {
"Kazuya Kinoshita": 1,
"Chizuru Ichinose": 2,
"Ruka Sarashina": 3,
"Mami Nanami": 4,
"Sumi Sakurasawa": 5,
"Mini Yaemori": 6
}
for i in range(1, chapters):
URL = 'https://kanojo-okarishimasu.fandom.com/wiki/Chapter_' + str(i)
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
ul = soup.find(id='Characters').findNext('ul')
oneHot = [0] * len(charMap)
for li in ul.findAll('li'):
charName = li.text
if charName in charMap:
oneHot[charMap[charName] - 1] = 1
charApps.append(oneHot)
# create df from columns
df_chars = pd.DataFrame(
charApps, columns=charMap.keys())
print(df_chars.head(10))
df_chars.to_csv('apps.csv', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment