legend ( loc = loc, title = loc ) fig, ax = plt. plot (,, label = 'TEST' ) # Place a legend to the right of this smaller subplot. subplots ( figsize = ( 6, 4 ), layout = 'constrained', facecolor = '0.7' ) ax. The legend is drawn outside the Axes on the (sub)figure. Specifying "outside" at the beginning of the loc keyword argument, Sometimes it makes more sense to place a legend relative to the (sub)figure legend ( bbox_to_anchor = ( 1.05, 1 ), loc = 'upper left', borderaxespad = 0. plot (, label = "test2" ) # Place a legend to the right of this smaller subplot. 102 ), loc = 'lower left', ncols = 2, mode = "expand", borderaxespad = 0. plot (, label = "test2" ) # Place a legend above this subplot, expanding itself to # fully use the given bounding box. subplot_mosaic (, ], empty_sentinel = "BLANK" ) ax_dict. Line, = ot(,, 'b') # Add some dataĪxes.legend((line,), ('Test',), loc='center') # Create legend on bottommost axisĪxes.Fig, ax_dict = plt. ![]() Gspec = gridspec.GridSpec(ncols=4, nrows=rows)Īxes.append(fig.add_subplot(gspec)) Rows = 17 # The larger the number here, the smaller the spacing around the legend The 'faux' axis is then turned off so only the legend shows. ![]() A 'faux' axis is created at the bottom to place the legend in a fixed spot. In the example below, the aim is to have four subplots arranged in a 2x2 fashion with the legend shown at the bottom. Using Matplotlib 2.2.2, this can be achieved using the gridspec feature. Plt.subplots_adjust(right=0.85) # Adjust the subplot to the right for the legend Magenta_patch = mpatches.Patch(facecolor='m', edgecolor='#000000')Ĭyan_patch = mpatches.Patch(facecolor='c', edgecolor='#000000')įig.legend(handles =, labels=labels, 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 ![]() 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)]
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |