
Red_patch = mpatches.Patch(facecolor='r', edgecolor='#000000') # This will create a red bar with black borders, you can leave out edgecolor if you do not want the bordersīlack_patch = mpatches.Patch(facecolor='k', edgecolor='#000000') Say you have four bars with different colours as r, m, c, and k, you can set the legend as follows: import matplotlib.patches as mpatches If you are using subplots with bar charts, with a different colour for each bar, it may be faster to create the artefacts yourself using mpatches. I recognize that sum(list_of_lists, ) is a really inefficient method to flatten a list of lists, but ① I love its compactness, ② usually is a few curves in a few subplots and ③ Matplotlib and efficiency? -) Return fig.legend(handles, labels, **kwdargs) # Call fig.legend with the keyword arguments, return the legend object Handles, labels = (sum(list_of_lists, ) for list_of_lists in tolohs) # a possible solution is to sum the sublists - we use unpacking # Finally, we need to concatenate the individual lists in the two # generating two tuples of lists of homogeneous stuff(tolohs), i.e., # so our first step is to transpose our data, # The legend needs a list of handles and a list of labels, # E.g., a figure with two axes, ax0 with two curves, ax1 with one curve Tuples_lohand_lolbl = (ax.get_legend_handles_labels() for ax in fig.axes)

# Generate a sequence of tuples, each contains

I have encapsulated the two tricky lines in a function, just four lines of code, but heavily commented def fig_legend(fig, **kwdargs):

it's really much much much easier from matplotlib.legend import _get_legend_handles_labelsįig.legend(*_get_legend_handles_and_labels(fig.axes). If you don't mind using a private method of the matplotlib.legend module. I tried the method proposed by the most up-voted and accepted answer, # fig.legend(lines, labels, loc='upper center', ncol=4)įig.legend(*a2.get_legend_handles_labels(), The two lines lines_labels = ĭeserve an explanation, see note 2 below. Perfect, otherwise see note no.1 below (there is a private If you want to stick with the official Matplotlib API, this is # Finally, the legend (that maybe you'll customize differently)įig.legend(lines, labels, loc='upper center', ncol=4) # So far, nothing special except the managed prop_cycle. # Note: we CALL the axes.prop_cycle to get an itertoools.cycleĬolor_cycle = plt.rcParams() # colored curves in different Axes, we need our own prop_cycle # each Axes has a brand new prop_cycle, so to have differently
#Subplot legend consistent legend location matplotlib code#
Now, if I've teased you enough, here it is the code from numpy import linspace I have noticed that none of the other answers displays an image with a single legend referencing many curves in different subplots, so I have to show you one. Lines, labels = ) for lol in zip(*lines_labels)] Plt.figlegend( line_labels, loc = 'lower center', borderaxespad=0.1, ncol=6, labelspacing=0., prop= ) #bbox_to_anchor=(0.5, 0.0), borderaxespad=0.1,įig.savefig('LSE_X=025.TL DR lines_labels =

#plt.rcParams = "Times New Roman"įig, axs = plt.subplots(6, 3, figsize=(12,16))#, constrained_layout=True) But this keeps a lot of white space between legend and subplots. I tried with removing the constrained_layout=True option. The lgend is now overlapping with the y-axis label I am having a problem with the location of legend. I am trying to create subplots on (6X3) grid.
