The world’s leading publication for data science, AI, and ML professionals.

How to add gradient background to Python plots

This article discusses a simple way to add gradient background to plots in Python

Image by author
Image by author

We may at times need to add multi-color background to plots to highlight a few sections of the plot. For example, we may highlight the periods of recession and recovery in red and green respectively. In this article, we’ll discuss a simple yet effective way to do this in Python. For Tableau users, this is similar to adding reference bands.

We’ll tell a story of how Nifty (a benchmark index of the Indian stock market) performed during Covid-19. We’ll highlight the period of slump and recovery in red and green respectively and also use Gradient Background to depict the volatility.

In the above code snippet, the variable nifty stores the closing values of Nifty from January 1, 2020 to September 18, 2020. The variable slump stores the closing values of Nifty from February 19, 2020 to May 18, 2020, this is the period where Nifty crashed due to Covid-19. The variable recovery stores the closing values of Nifty from May 18, 2020 to September 18, 2020, this is the period where Nifty began recovering from Covid-19. Below is a snapshot of the nifty data frame.

Nifty data frame (Image by author)
Nifty data frame (Image by author)

To understand what’s goes into the plot, we’ll plot the time series without any background colors and tell a story about Nifty’s journey through Covid-19.

Image by author
Image by author

In the above plot, the periods of slump and recovery are highlighted in red and green respectively. There are a few annotations to highlight the major events. There is also an annotation in a bigger font that highlights the number of days of carnage. What if we need to depict the volatility during the periods of slump and recovery? We can do this by using gradient background. Though this method might be computationally expensive, using it can add a new dimension to the story. We’ll look at the code to add gradient background to the above plot.

Image by author
Image by author

How is the gradient background added?

  1. We calculated the 3-day moving average of the values in the period of slump and recovery.
  2. The moving average values are normalized within a range of 0.15 to 1 to act as alpha values for the red and green vertical lines (axvline) drawn as the background to the plot. For the red lines, the higher the normalized moving average value, the lighter would be the line. For the green lines, the higher the normalized moving average value, the darker would be the line.
  3. Each normalized value is repeated 10 times as the vertical lines would be plotted with x-tick incrementing by 0.1 i.e. every single x-tick has 10 vertical lines.
  4. The last value of the normalized moving averages is repeated 20 times to ensure the number of values are same as that of the original time series, as moving average calculation will result in a reduction of number of values in the time series.
  5. Vertical lines are drawn as the background of the plot with x-tick incrementing by 0.1.

In the above plot, we can see alternate light and dark green bands at the initial stages of recovery showing that the initial stages of recovery were more volatile than the later ones. We can also see a darker red band where Nifty made its bottom at 7,610.25.

This brings this article to an end. We’ve discussed how to add a gradient background using multiple colors to a Python plot. This technique can be used to highlight business cycles. Keeping the alpha value of the vertical lines constant will result in a multi-color background instead of a gradient background.

Know more about my work at https://ksvmuralidhar.in/


Related Articles