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()])
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')

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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With