2

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.

zacha2
  • 223
  • 2
  • 16
  • Yes, a patch has a single color (or two if you count face- and edge-color separately). I do have slight problems understanding the desired outcome. I.e. what does a a patch that is made up of n colors supposed to look like? – ImportanceOfBeingErnest Sep 04 '19 at 13:45
  • The desired outcome is a patch, which is for example: 1/3 blue 1/3red and 1/3green – zacha2 Sep 04 '19 at 19:45
  • Oh, that means 3 patches, really. – ImportanceOfBeingErnest Sep 04 '19 at 19:47
  • Yes, I was thinking about that too. But it seems like rather extensive solution due individual patch creation and positioning. So I was wondering if there's a way to make it work with a list of colors per label. – zacha2 Sep 04 '19 at 19:56

1 Answers1

5

Patches have a facecolor and an edgecolor. So you can colorize those differently, like

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#one color per patch
#define class and colors
 #multiple colors per patch
colors = [['#01FF4F','#00FFff'], 
           ['#FFEB00','#FFFF00'], 
            ['#FF01D7','#FF00aa'], 
             ['#5600CC','#FF00EE']]
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(facecolor=legend_dict[key][0], 
                                  edgecolor=legend_dict[key][1], label=key)
        patchList.append(data_key)

#plotting
plt.gca()
plt.legend(handles=patchList,ncol=len(categories), fontsize='small')
plt.show()

enter image description here

Or if you really want different patches of different color you can create tuples of those patches and supply them to the legend.

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerTuple

#one color per patch
#define class and colors
 #multiple colors per patch
colors = [['#01FF4F','#00FFff'], 
           ['#FFEB00','#FFFF00'], 
            ['#FF01D7','#FF00aa'], 
             ['#5600CC','#FF00EE']]
categories = ['A','B','C','D']
#create dict
legend_dict=dict(zip(categories,colors))
#create patches
patchList = []
for cat, col in legend_dict.items():
    patchList.append([mpatches.Patch(facecolor=c, label=cat) for c in col])


plt.gca()
plt.legend(handles=patchList, labels=categories, ncol=len(categories), fontsize='small',
           handler_map = {list: HandlerTuple(None)})

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712