I'm working on a plot including line & bar chart as well as vertical spans (axvspan). The chart is basically complete, but it won't let me display axvspan and the bar chart (=precipitation) in the legend. I just can't find the mistake. I put the different labels in the axvspan line, but it doesn't show it in the legend. Anyone can please help me out with that?
Here's the code:
x = np.arange(1966, 2021, 1)
y1 = annual_temp['temp med']
y2 = pr_1966_2020['precipitation']
fig,ax = plt.subplots()
ax.bar(x, y2, color="blue", label='precipitation', width=0.5)
ax.axvspan(1972, 1973, color='magenta', alpha=0.2, label='Strong El Niño') #se
ax.axvspan(1987, 1988, color='magenta', alpha=0.2)
ax.axvspan(1991, 1992, color='magenta', alpha=0.2)
ax.axvspan(1982, 1983, color='magenta', alpha=0.7, label='Very strong El Niño') #vse
ax.axvspan(1997, 1998, color='magenta', alpha=0.7)
ax.axvspan(2015, 2016, color='magenta', alpha=0.7)
ax.axvspan(1973, 1974, color='cyan', alpha=0.2, label= 'Strong La Niña') #sl
ax.axvspan(1975, 1976, color='cyan', alpha=0.2)
ax.axvspan(1988, 1989, color='cyan', alpha=0.2)
ax.axvspan(1998, 1999, color='cyan', alpha=0.2)
ax.axvspan(1999, 2000, color='cyan', alpha=0.2)
ax.axvspan(2007, 2008, color='cyan', alpha=0.2)
ax.axvspan(2010, 2011, color='cyan', alpha=0.2)
ax.set_ylabel("Precipitation (in mm)",color="b",fontsize=12)
ax.set_xlabel("Years",fontsize=12)
plt.ylim(0, 40)
ax2=ax.twinx()
ax2.plot(x, y1, color="red", label='temperature')
ax2.set_ylabel("Temperature (in °C)",color="r",fontsize=12)
plt.ylim(0, 20)
plt.suptitle('Cerro Moreno, Antofagasta, Chile (112 m a.s.l.)', y=1.05, fontsize=18)
plt.title('1966-2020 with (Very) Strong El Niño and Strong La Niña events\n24°S, 70°W\n3.59 mm 17.2°C', fontsize=10)
plt.legend(bbox_to_anchor=(1.1,0.7), loc="upper left")
plt.gcf()