I am trying to plot a pandas groupby
object using the code fil.groupby('imei').plot(x=['time'],y = ['battery'],ax=ax, title = str(i))
The problem is the plot legend lists ['battery']
as the legend value. Given it's drawing a line for each item in the groupby
object, it makes more sense to plot those values in the legend instead. However I'm not sure how to do that. Any help would be appreciated.
Data
time imei battery_raw
0 2016-09-30 07:01:23 862117020146766 42208
1 2016-09-30 07:06:23 862117024146766 42213
2 2016-09-30 07:11:23 862117056146766 42151
3 2016-09-30 07:16:23 862117995146745 42263
4 2016-09-30 07:21:23 862117020146732 42293
Full code
for i in entity:
fil = df[(df['entity_id']==i)]
fig, ax = plt.subplots(figsize=(18,6))
fil.groupby('imei').plot(x=['time'],y = ['battery'],ax=ax, title = str(i))
plt.legend(fil.imei)
plt.show()
Current plot
Pandas' groupby() allows us to split data into separate groups to perform computations for better analysis. In this article, you'll learn the “group by” process (split-apply-combine) and how to use Pandas's groupby() function to group data and perform operations.
What is the GroupBy function? Pandas' GroupBy is a powerful and versatile function in Python. It allows you to split your data into separate groups to perform computations for better analysis.
Slightly tidied data:
date time imei battery_raw
0 2016-09-30 07:01:23 862117020146766 42208
1 2016-09-30 07:06:23 862117020146766 42213
2 2016-09-30 07:11:23 862117020146766 42151
3 2016-09-30 07:16:23 862117995146745 42263
4 2016-09-30 07:21:23 862117995146745 42293
Complete example code:
import matplotlib.pyplot as plt
fil = pd.read_csv('imei.csv', sep=r'\s*', engine='python')
fig, ax = plt.subplots(figsize=(18,6))
for name, group in fil.groupby('imei'):
group.plot(x=pd.to_datetime(group['time']), y='battery_raw', ax=ax, label=name)
plt.show()
The x-values have to be converted to datetime for plotting to come out right, as usual. You could do that in the dataframe, too.
Result, labeled by imei:
(NOTE: edited to get rid of an oddity I tripped over the first time. If you pass a list as the y
argument to group.plot
, the list IDs will be used as the line labels, presumably as a handy default for when you're plotting several dependent variables at once.
#for name, group in fil.groupby('imei'):
# group.plot(x=['time'], y=['battery_raw'], ax=ax, label=name)
)
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