zacha2
zacha2

Reputation: 223

matplotlib.patches: One patch with mulitple colors

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.

Upvotes: 2

Views: 2087

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339170

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

Upvotes: 6

Related Questions