Bubnoff
Bubnoff

Reputation: 4097

pandas - matplotlib: set xtick frequency with time data

I cannot figure out how to change the xtick frequency with time data. Errors abound regardless of the recipe.

Sample raw data:

datetime            branch
2019-05-07 14:39:14 NOWHEREVILLE
2019-05-07 16:17:39 NOWHEREVILLE
2019-05-07 16:17:41 NOWHEREVILLE
2019-05-07 16:26:42 NOWHEREVILLE
2019-05-07 16:26:50 NOWHEREVILLE

Resampled by hour:

hourly_count = circ.resample('H').count()
hourly_count = hourly_count.rename(columns={'branch': 'circulation'})

hourly_count.head()

Output:

datetime            circulation 
2019-05-07 14:00:00 1
2019-05-07 15:00:00 0
2019-05-07 16:00:00 5
2019-05-07 17:00:00 0
2019-05-07 18:00:00 0

If I just do this: hourly_count.plot(), maybe change the plot size, everything works. But the frequency of the xtick marks is not zoomed in enough.

All the recipes I see on SO have you explicitly setting the x-axis. I have yet to find a recipe for this that doesn't throw an error.

Example:

hourly_count.plot(x=hourly_count.index, y='circulation', kind='line', lw=0.75, c='r') 

Throws ( snippet ):

KeyError                                  Traceback (most recent call last)
<ipython-input-20-7f11c92c2c61> in <module>()
----> 1 hourly_count.plot(x=hourly_count.index, y='circulation', kind='line', lw=0.75, c='r')

/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2939                           fontsize=fontsize, colormap=colormap, table=table,
   2940                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2941                           sort_columns=sort_columns, **kwds)
   2942     __call__.__doc__ = plot_frame.__doc__
   2943 

/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   1975                  yerr=yerr, xerr=xerr,
   1976                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 1977                  **kwds)
   1978 
   1979 

I am following along here using my own data: Advanced plotting with Pandas

Everything works until I get to the plot command above.

Upvotes: 0

Views: 206

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Update:

fig, ax = plt.subplots()
plt.plot(df.index, df['circulation'])
ax.set_xlim(df.index.min() - pd.Timedelta(hours=5), df.index.max() + pd.Timedelta(hours=5))
mytime = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(mytime)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=1))
fig.autofmt_xdate()

Output:

enter image description here

IIUC, you can use set_xlim with timedelta adjustments:

print(df)

             datetime  circulation
0 2019-05-07 14:00:00            1
1 2019-05-07 15:00:00            0
2 2019-05-07 16:00:00            5
3 2019-05-07 17:00:00            0
4 2019-05-07 18:00:00            0

df1 = df.set_index('datetime')
ax = df1.plot()
ax.set_xlim(df1.index.min() - pd.Timedelta(hours=5), df1.index.max() + pd.Timedelta(hours=5))

Output:

enter image description here

Upvotes: 1

Related Questions