4

I am plotting two pandas series. The index is a date (1-1 to 12-31)

s1.plot()
s2.plot()

pd.plot() interprets the dates and assigns them to axis values as such:

enter image description here

I would like to modify the major ticks to be the 1st of every month and minor ticks to be the days in between

This works:

%matplotlib notebook

import matplotlib as mpl
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('data.csv')

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%m-%d')
s2014max = df2014.groupby(['Date'], sort=True)['Data_Value'].max()/10
s2014min = df2014.groupby(['Date'], sort=True)['Data_Value'].min()/10

#remove the leap day and convert to datetime for plotting
s2014min = s2014min[s2014min.index != '02-29']
s2014max = s2014max[s2014max.index != '02-29']
dateslist = s2014min.index.tolist()

dates = [pd.datetime.strptime(date, '%m-%d').date() for date in dateslist]

plt.figure()

ax = plt.gca()
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator())

monthFmt = mdates.DateFormatter('%b')
dayFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(monthFmt) 
ax.xaxis.set_minor_formatter(dayFmt)  
ax.tick_params(direction='out', pad=15)

s2014min.plot()
s2014max.plot()

This results in no ticks:

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
ctd25
  • 730
  • 1
  • 11
  • 22

2 Answers2

3

A possible way is to use matplotlib for plotting the dates instead of pandas.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np

dates = pd.date_range("2016-01-01", "2016-12-31" )
y = np.cumsum(np.random.normal(size=len(dates)))
df = pd.DataFrame({"Dates" : dates, "y": y})

fig, ax = plt.subplots()
ax.plot_date(df["Dates"], df.y, '-')

ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator())
monthFmt = mdates.DateFormatter('%b')
ax.xaxis.set_major_formatter(monthFmt)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I had to convert the dates from strings using strptime and also remove the leap day and then this worked nicely. Thank you! – ctd25 Mar 19 '17 at 21:26
2

You were so close! All you needed to do was add the formatters similar to how the other answer did it. Here is a working sample similar to your code (note I did mine in ipython notebook hence the %matplotlib inline).

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
from random import random
y = [random() for i in range(25)]
x = [(datetime.now() - timedelta(days=i)) for i in range(25)]
x.reverse()
s = pd.Series(y, index=x)  # NOTE: S, not df, since you said you were using series
# format the ticks
ax = plt.gca()
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator())

monthFmt = mdates.DateFormatter('%b')
dayFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(monthFmt)  # This is what you needed
ax.xaxis.set_minor_formatter(dayFmt)  # This is what you needed
ax.tick_params(direction='out', pad=15)
# format the coords message box
s.plot(figsize=(10,3))

which will look like this: enter image description here

Nick Brady
  • 6,084
  • 1
  • 46
  • 71
  • 1
    Thanks Nick. I tried adding that and am still seeing the same result. I updated my original question to include all of my code, including the dateformatters, if you have a second to take another look – ctd25 Mar 19 '17 at 15:09
  • 1
    Glad you got it figured out :) – Nick Brady Mar 20 '17 at 02:27
  • for this to work you would need to add the option `x_compat=True` to the `s.plot` call. – Oleksandr Dec 01 '21 at 20:29