2016-02-14 3 views
5

ich eine Figur Plotten, wo das Standardformat zeigt sich als:Matplotlib Datum Manipulation so dass das Jahr tick zeigen alle 12 Monate bis

Year tick shows up every 12 months, but months show only every 3 months

Ich möchte es so modifizieren, dass der Monat Zecken tauche alle 1 Monat auf, behalte aber das Jahr. Mein aktueller Versuch ist dies:

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b-%y') 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_major_locator(months) 
ax.xaxis.set_major_formatter(monthsFmt) 

aber es produziert nicht das richtige Ergebnis:

Not right

Wie genau muss ich es so modifizieren, dass es wie das erste zeigt sich aber mit den Monaten Ticks erscheinen jeden Monat?

Antwort

7

Herausgearbeitet eine Lösung, die Monate in die kleinen Zecken kleben soll und Jahre als das Haupt behalten.

z.

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b') 
yearsFmt = mdates.DateFormatter('\n\n%Y') # add some space for the year label 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_minor_locator(months) 
ax.xaxis.set_minor_formatter(monthsFmt) 
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90) 
ax.xaxis.set_major_locator(years) 
ax.xaxis.set_major_formatter(yearsFmt) 

Ergebnisse in: enter image description here

Verwandte Themen