Параметр, показывающий годы во втором ряду, заключается в использовании основных и второстепенных меток.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, YearLocator, DateFormatter
ix = pd.date_range('1/1/2017', '11/1/2018', freq='D')
vals = np.random.randn(len(ix))
s = pd.DataFrame({'Values': vals}, index=ix)
fig, ax = plt.subplots(figsize=[8,6])
ax.plot(s, lw=1)
ax.xaxis.set_major_locator(YearLocator())
ax.xaxis.set_major_formatter(DateFormatter("\n%Y"))
ax.xaxis.set_minor_locator(MonthLocator((1,4,7,10)))
ax.xaxis.set_minor_formatter(DateFormatter("%b"))
plt.show()
Если вам нужны второстепенные тики для чего-то еще, то следующие отформатируют только основные тики - с тем же результатом.Здесь вы должны использовать FuncFormatter для определения формата в зависимости от месяца.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, DateFormatter
from matplotlib.ticker import FuncFormatter
ix = pd.date_range('1/1/2017', '11/1/2018', freq='D')
vals = np.random.randn(len(ix))
s = pd.DataFrame({'Values': vals}, index=ix)
fig, ax = plt.subplots(figsize=[8,6])
ax.plot(s, lw=1)
monthfmt = DateFormatter("%b")
yearfmt = DateFormatter("%Y")
def combinedfmt(x,pos):
string = monthfmt(x)
if string == "Jan":
string += "\n" + yearfmt(x)
return string
ax.xaxis.set_major_locator(MonthLocator((1,4,7,10)))
ax.xaxis.set_major_formatter(FuncFormatter(combinedfmt))
plt.show()
Результат в обоих случаях одинаков:
![enter image description here](https://i.stack.imgur.com/cka0u.png)