Plotting TV series through time: scatter plot

Viewed 56

have this dataset

ID  TV            Start
55  gameofthrones   21:34:00
20  beverlyhills    21:34:00
66  modernfamiily   00:34:00
65  housemd         01:34:00
69  hunters         01:34:00
... ... ...
65  housemd         16:34:00
38  mrroobot        16:34:00

I need to plot TV Series in ascending order, from 00:00 to 23:59, based on Start (time). I have tried as follows

import matplotlib.pyplot as plt 

plt.figure()
plt.plot(df['TV'],df['Start'])

and it works. However, I would like to consider a scatter plot, as sometimes lines overlaps to each other, making difficult to visualise the order. How can I do?

1 Answers

Use, pd.to_datetime to convert the values of Start column into datetime, then sort the dataframe using this column:

# sorting the dataframe by the start time
df['Time'] = pd.to_datetime(df['Start'], format='%H:%M:%S').dt.time
df = df.sort_values(by='Time').drop('Time', 1)

# Drawing the sample Scatter plot
plt.figure()
plt.scatter(df['Start'], df['TV'], s=100)
plt.show()

# Dataframe after sorting by Time:
# print(df)

 ID             TV     Start
 66  modernfamiily  00:34:00
 65        housemd  01:34:00
 69        hunters  01:34:00
 65        housemd  16:34:00
 38       mrroobot  16:34:00
 55  gameofthrones  21:34:00
 20   beverlyhills  21:34:00

Example, Scatter plot:

enter image description here

Related