Reputation: 1572
Hi I am trying to plot a categorical data which is description:
data_df = pd.DataFrame({'Date': ['2018-09-14 00:00:22',
'2018-09-14 00:01:46',
'2018-09-14 00:01:56',
'2018-09-14 00:01:57',
'2018-09-14 00:01:58',
'2018-09-14 00:02:05'],
'userID': [33, 33, 33, 20, 20, 20],
'device': ['LIGHT', 'LIGHT', 'FAN', 'LIGHT', 'FAN', 'FAN'],
'description': ['ON', 'DIM', 'ON', 'ON', 'ON', 'OFF']})
data_df
What I want to is to plot them by grouping the 'device' but before that I want to subplot by rows the 'userID' so that they are separated each by 'userID' and each line plots are grouped based on the 'device' name.
I also tried grouping by and plot but it say my description is not numeric since it is categorical. Date is the x-axis, and description as y-axis.
Upvotes: 1
Views: 79
Reputation: 1726
It looks like you might be trying to plot the description
of userID
and device
groups over time. Obviously, description
needs to be converted to a numeric variable, so I took the liberty of coding ON as 1, DIM as 0.5, and OFF as 0. The following code should solve your problem.
data_df['Date'] = pd.to_datetime(data_df['Date'])
def desc_num(x):
if x == 'ON':
return 1
elif x == 'DIM':
return 0.5
else:
return 0
data_df['desc_num'] = data_df['description'].apply(desc_num)
## Creating groups of `userID` and `device`
groups = data_df.groupby(['userID', 'device'])
for g in groups:
plt.plot(g[1]['Date'], g[1]['desc_num'])
plt.xlabel('Time')
plt.ylabel('description Status')
plt.title('Time Series of userID: {0}, for device: {1}'.format(g[1]['userID'][0], g[1]['device'][0]))
plt.show()
plt.close()
Output (you should see such a plot for every subgroup):
Upvotes: 1