Get Started

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 is a 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()

This exercise is part of the course

Visualizing Time Series Data in Python

IntermediateSkill Level
4.7+
15 reviews

Visualize seasonality, trends and other patterns in your time series data.

You will learn how to leverage basic plottings tools in Python, and how to annotate and personalize your time series plots. By the end of this chapter, you will be able to take any static dataset and produce compelling plots of your data.

Exercise 1: Welcome to the course!Exercise 2: Load your time series dataExercise 3: Test whether your data is of the correct typeExercise 4: Plot your first time seriesExercise 5: Your first plot!Exercise 6: Specify plot stylesExercise 7: Display and label plotsExercise 8: Customize your time series plotExercise 9: Subset time series dataExercise 10: Add vertical and horizontal markersExercise 11: Add shaded regions to your plot

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free