I can create a custom legend with a color per category this way:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
#one color per patch
#define class and colors
colors = ['#01FF4F', '#FFEB00', '#FF01D7', '#5600CC']
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
#create patches
patchList = []
for key in legend_dict:
data_key = mpatches.Patch(color=legend_dict[key], label=key)
patchList.append(data_key)
#plotting
plt.gca()
plt.legend(handles=patchList,ncol=len(categories), fontsize='small')
plt.show()
Now I want to create a legend, where each patch is made up of n colors.
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
#multiple colors per patch
colors = [['#01FF4F','#01FF6F'], ['#FFEB00','#FFEB00'], ['#FF01D7','#FF01D7','#FF01D7'], ['#5600CC']]
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
print(legend_dict)
The patch for category A should have the colors '#01FF4F'and '#01FF6F'. For category B it's '#FFEB00' and '#FFEB00' and so on.