Get startedGet started for free

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).

This exercise is part of the course

Visualizing Time Series Data in Python

View Course

Exercise 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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()
Edit and Run Code