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 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). Marker = dict(color = c),#df_type_2įig.update_layout(height = 600, width = 800, margin = dict(l = 10, r = 10, t = 30, b = 10)) įig.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. Marker = dict(color = c),#df_type_1ĭf_color = df_type_2 = c] df = pd.DataFrame(]],ĭf_color = df_type_1 = c] I'm aiming to include the correct color in the legend. The colors for each point works well for both subplots.īut the legend displays the same color for each Category. Each unique value in Category has an assigned color. Using below, there are two subplots that are taken from Type. This all works fine but I'm hoping to include a legend that describes each color and category. I've included a function that assigns a specific color to each unique category. The following figure produces two subplots using scattermapbox in Plotly.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |