

There are a couple useful ways to put text boxes outside of the plot/figure. Leg = plt.legend(, loc = "center left", bbox_to_anchor = (1, 0.5), numpoints = 1)įig.savefig('legend_test.png', bbox_extra_artists=(leg,), bbox_inches='tight') # Calling the handles and labels to create the legend, where the handles are the club and circle created previously, and the labels are what the markers are labeled in the legend. This is the creation of the proxy artists.Ĭlub = Line2D(range(1), range(1), color="white", marker=r'$\clubsuit$', markersize=10, markerfacecolor="black")Ĭircle = Line2D(range(1), range(1), color="white", marker='o', markersize=10, markerfacecolor="green") # Using Line2D to create the markers for the legend.
#MATPLOTLIB SUBPLOT LEGEND CODE#
The code below shows a simple example of this. To create a custom legend where the user has more control over what is put in the legend, the user can use proxy artists.

Text boxes are explained in the next section. Note that the right plot utilizes a text box below the legend.

With the large number of entries in the legend, the user might prefer a legend with only one shape per type of point, with the colors explained in a separate text box as shown in the right plot. One case where a custom legend might be desired is a scatter plot where a lot of different colors are used for a shape, but the user only wants one shape to appear on the legend.Such a scatter plot is shown in the left plot below, with a matplotlib generated legend.

Matplotlib generates the legend based on what is inside the plot, but a custom legend can be created regardless of what is in the plot. Sometimes the user might want to create a customized legend rather than having matplotlib generate the legend. Legends with long descriptors should be avoided if possible, as matplotlib can cut off the legends in these cases even with the bbox_extra_artists=() and bbox_inches='tight'. Note that the user might have to experiment with positional arguments to get the legend/title to be in the desired position. Here is the plot generated by the above code:ĭocumentation for figlegend(): Matplotlib Figlegendĭocumentation for suptitle(): Matplotlib Suptitle # Note bbox_extra_artists includes both the title and the legend so that neither are cut offįig.savefig('test.png', bbox_extra_artists=(leg, suptitle,), bbox_inches='tight') Suptitle = plt.suptitle('Example Plot', x = 0.45, y = 0.87, fontsize=18) Legend created with figlegend(), title with suptitle() # Creating legend and title for the figure. # Plotting example plots with line x=y of different colors # Creating the figure with four subplots, 2 per column/row An example plot is shown below.īelow is some simple code that plots subplots with only one title and legend for the whole figure: import matplotlib
#MATPLOTLIB SUBPLOT LEGEND HOW TO#
This section will show how to accomplish the second choice of one legend and one title to account for all of the subplots. When using subplots, the user might choose to have a legend per subplot as well as a title per subplot, or the user might choose only to have one legend and one title to account for all subplots. Legends outside of Plot/Figure for Subplots Leg = plt.legend(loc='center left', bbox_to_anchor(1,0.815), numpoints=1)įig.savefig(outfile, bbox_extra_artists=(leg,), bbox_inches='tight') # when savefig is called (need not be right after the legend is created), bbox_extra_artists=(leg,) and bbox_inches='tight' ensure that the legend does not get cut off when the figure is saved. # numpoints ensures that two points don't appear in the legend entry. # loc can also change the legend location Having bbox_to_anchor(1, 0.5) would put the legend halfway down the y axis. # bbox_to_anchor(1,0.815) moves the plot outside the figure, taking positional arguments (x,y). To generate a legend like that in the right plot, create the legend using the code below. The right plot looks nicer, with the legend outside of the plot and with no need to set transparency since the legend will never overlap the data. fancybox=True and set_alpha(0.5) made the legend slightly transparent so these points could be viewed. Matplotlib tried to put the legend in the best location within the plot, but the legend still overlapped the data. The left plot was generated with plt.legend(loc = 'best', numpoints=1, fancybox=True) and leg.get_frame().set_alpha(0.5). The following sections show how to use matplotlib to create various plots or plot elements.īelow shows a side by side comparison of what legends look like inside and outside the plot.
