Alexis
Alexis

Reputation: 2294

Repeated x values on a pandas plot

I have the following dataset:

my_df = pd.DataFrame({'id':[1,2,3,4,5,6,7,8],
                      'date':['2019-01-01 07:59:54','2019-01-01 08:00:07','2019-01-01 08:00:07',
                              '2019-01-02 08:00:14','2019-01-02 08:00:16','2019-01-02 08:00:24',
                              '2019-01-03 08:02:38','2019-01-03 08:50:14'],
                      'machine':['A','A','B','C','B','C','D','D']})
my_df['date'] = pd.to_datetime(my_df['date'],infer_datetime_format=True)
my_df

And I want to plot the unique values per machine per day, so I tried this code:

fig, ax = plt.subplots(figsize=(12,6))
# data.plot(ax=ax)
my_df.groupby(my_df['date'].dt.date)['machine'].nunique().plot(ax=ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d %m'))
plt.show()

But for some reason, each day is repeated as many times as there are machines. I expected the x axis to go from 2019-01-01, 2019-01-02 and 2019-01-03, that is only 3 values on the x axis. Any help on this issue will be greatly appreciated.

enter image description here

Upvotes: 0

Views: 144

Answers (1)

Corralien
Corralien

Reputation: 120399

The problem is the key used in groupby. my_df['date'].dt.date is not a datetime anymore. You have to use pd.Grouper(key='date', freq='D') instead:

fig, ax = plt.subplots(figsize=(12,6))
my_df.groupby(pd.Grouper(key='date', freq='D'))['machine'].nunique().plot(ax=ax)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d %m'))
plt.show()

enter image description here

Upvotes: 1

Related Questions