Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create eventplot from pandas long format

I am having issue to get a pandas dataframe with several observations into the correct format to display an eventplot. The 'creator' column should be the label used to distinguish the datasets.

import pandas as pd
from matplotlib import pyplot as plt

data = {
    "creator": [1, 2, 1, 1, 2],
    "creationdate": ["2019-03-13 16:43:55", "2019-03-13 16:43:55", "2019-03-15 15:52:05",
                   "2019-03-16 15:52:05", "2019-03-17 15:52:05"]
}

df = pd.DataFrame(data)
df["creationdate"] = pd.to_datetime(df["creationdate"])

# df
#   creator        creationdate
#0        1 2019-03-13 16:43:55
#1        2 2019-03-13 16:43:55
#2        1 2019-03-15 15:52:05
#3        1 2019-03-16 15:52:05
#4        2 2019-03-17 15:52:05

# Group by creator
grouped = df.groupby("creator")

# How can the data now be reshaped to actually display the plot
# ...

# TypeError: Invalid comparison between dtype=datetime64[ns] and int
fig = plt.eventplot(grouped)
plt.show()

I tried to iterate over the grouped array to extract the individual groups but this seems way to complex and unnecessary.

data = np.array([grouped.get_group(1)["creationdate"].to_numpy(), grouped.get_group(2)["creationdate"].to_numpy()])
like image 912
Kilian Avatar asked Apr 25 '26 14:04

Kilian


2 Answers

  • Use enumerate on the groupby object to index the list of colors.
import pandas as pd
import matplotlib.pyplot as plt

# load data
data = {'creator': [1, 2, 1, 1, 2], 'creationdate': ['2019-03-13 16:43:55', '2019-03-13 16:43:55', '2019-03-15 15:52:05', '2019-03-16 15:52:05', '2019-03-17 15:52:05']}

df = pd.DataFrame(data)

# convert column to a datetime dtype
df['creationdate'] = pd.to_datetime(df['creationdate'])

# create the fig / axes
fig, ax = plt.subplots(figsize=(10, 4))

# iterate through each group and plot
colors = ['blue', 'red']
for i, (label, data) in enumerate(df.groupby('creator')):
    ax.eventplot('creationdate', colors=colors[i], data=data, label=label)

ax.legend(title='Creator', bbox_to_anchor=(1, 1.02), loc='upper left')
ax.set(xlabel='Datetime', ylabel='Value', title='Eventplot')

enter image description here

like image 141
Trenton McKinney Avatar answered Apr 28 '26 02:04

Trenton McKinney


You can use for key in grouped.groups.keys(): to quickly iterate over each group key

import pandas as pd
from matplotlib import pyplot as plt

data = {
    "creator": [1, 2, 1, 1, 2],
    "creationdate": ["2019-03-13 16:43:55", "2019-03-13 16:43:55", "2019-03-15 15:52:05",
                   "2019-03-16 15:52:05", "2019-03-17 15:52:05"]
}

df = pd.DataFrame(data)
df["creationdate"] = pd.to_datetime(df["creationdate"])

# df
#   creator        creationdate
#0        1 2019-03-13 16:43:55
#1        2 2019-03-13 16:43:55
#2        1 2019-03-15 15:52:05
#3        1 2019-03-16 15:52:05
#4        2 2019-03-17 15:52:05

# Group by creator
grouped = df.groupby("creator")

print(df)

# How can the data now be reshaped to actually display the plot
# ...

# TypeError: Invalid comparison between dtype=datetime64[ns] and int
for key in grouped.groups.keys():
    # Color them differently
    if key == 1:
        color = "blue"
    elif key == 2:
        color = "red"
    pos = grouped.get_group(key)["creationdate"].values
    fig = plt.eventplot(pos, colors=color)
plt.show()
like image 23
Ian Graham Avatar answered Apr 28 '26 03:04

Ian Graham