Ich versuche, die Frühlingsmonate in einem Pandas-Grundstück mit Nebengeschäften zu schattieren. Aber es schattiert nur den letzten Teilplot. Wie kann ich alle Parzellen beschatten?Pandas: wenden Sie axvspan auf alle Nebenstellen
Ich benutze axvspan
, um zwischen dem 1. April und 30. Juni jedes Jahr durch das Durchschleifen eines gruppierten Datenrahmens dieser Enddaten zu schattieren.
Hier ist das Ergebnis.
import matplotlib.pyplot as plt
recent = daily[daily.Date.dt.year >= 2000]
# Get only April 1 and Jume 30 each year
spring_months = recent[((recent.Date.dt.month == 4) & (recent.Date.dt.day == 1)) | ((recent.Date.dt.month == 6) & (recent.Date.dt.day == 30))]['Date']
# Make pivot table with data, one measuring station per column.
recent = recent.pivot(index='Date', columns='Station', values = 'Niveau(m)')
recent.plot(figsize=[7,50], subplots=True)
plt.xlim(xmax='2017-07-10')
# Group the spring end-dates by year
years = spring_months.drop_duplicates().groupby(spring_months.dt.year)
# Loop through groups and add axvspan between April 1 and June 30 each year
for n, g in years:
plt.axvspan(g.iloc[0], g.iloc[1], facecolor='g', alpha=0.5)
if g.iloc[0].year == 2016:
break
Das war es, und sehr effizient auch. – robroc