CommencerCommencer gratuitement

Add shaded regions to your plot

When plotting time series data in Python, it is also possible to highlight complete regions of your time series plot. In order to add a shaded region between January 1, 1936 and January 1, 1950, you can use the command:

ax.axvspan('1936-01-01', '1950-01-01', color='red' , alpha=0.5)

Here we specified the overall transparency of the region by using the alpha argument (where 0 is completely transparent and 1 is full color).

Cet exercice fait partie du cours

Visualizing Time Series Data in Python

Afficher le cours

Instructions

  • Use the .axvspan() method to add a vertical red shaded region between the dates of January 1, 1900 and January 1, 1915 with a transparency of 0.3.
  • Use the .axhspan() method to add a horizontal green shaded region between the values of 6 and 8 with a transparency of 0.3.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Plot your the discoveries time series
ax = discoveries.plot(color='blue', fontsize=6)

# Add a vertical red shaded region
ax.____('1900-01-01', ____, color=____, alpha=____)

# Add a horizontal green shaded region
ax.____(6, ____, color=____, alpha=____)

plt.show()
Modifier et exécuter le code