Add legend and legend color to stack plot - Python Mathplotlib Module

Author: Al-mamun Sarkar Date: 2020-04-02 17:08:30

Add legend and legend color to stack plot - Python Mathplotlib Module. The following codes show how to add legend and legend color on Stack graph using python mathplotlib module.

 

Import Module:

import matplotlib.pyplot as plt

 

Create Data:

days_data = [1,2,3,4,5]

sleeping_data = [7,8,6,11,7]
eating_data =   [2,3,4,3,2]
working_data =  [7,8,7,2,2]
playing_data =  [8,5,7,8,13]

 

Add legend and legend color to stack plot:

plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.plot([],[],color='c', label='Eating', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days_data, sleeping_data, eating_data, working_data, playing_data, colors=['m', 'c', 'r', 'k'])
plt.title("Welcome to stack plotting tutorial")
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.legend()
plt.show()