Control the color in stacked area chart

logo of a chart:StackedArea

Once you understand how to create a stacked area chart and which baseline option to choose, it’s highly recommended to custom the colors of your chart. This post explains how to customize colors.

You can change the colors in your stacked area chart with the colors parameter of the stackplot() function. Here I propose 2 solutions: the first one is to use a common color palette, the second is to pick up your colors one by one.

# libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
 
# Your x and y axis
x = range(1,6)
y = [ [10,4,6,5,3], [12,2,7,10,1], [8,18,5,7,6] ]
 
# use a known color palette
pal = sns.color_palette("Set1")
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4 )
plt.legend(loc='upper right')
plt.show()
 
# create your palette
pal = ["#9b59b6", "#e74c3c", "#34495e", "#2ecc71"]
plt.stackplot(x,y, labels=['A','B','C'], colors=pal, alpha=0.4 )
plt.legend(loc='upper right')
plt.show()

Timeseries

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!