Matplotlib Pyplot Legend Outside

Bbox means bounding box that accommodates the legend.
Matplotlib pyplot legend outside. The location of the legend can be specified by the keyword argument loc please see the documentation at legend for more details. When we want to put legend somewhere in a figure using matplotlib most of the time the option loc best will produce the desired results. In this article we will show you some examples of legends using matplotlib. A legend can be placed inside or outside the chart and the position can be moved.
Import numpy as np import matplotlib pyplot as plt generate random data for plotting x np. Bbox extra artists and bbox inches to prevent legend box from being cropped legend could be placed outside of the plot in the matplotlib by using bbox to anchor. For example if you want your axes legend located at the figure s top right hand corner instead of the axes corner simply specify the corner s location. The following code shows how to do it.
This function is used to specify the location and the exact coordinates to display the legend in the figure. The bbox to anchor keyword gives a great degree of control for manual legend placement. The font size of the legend. Linspace 0 0 100 50 y2 x 2 y3 x 3 y4 x 4 y5 x 5 y6 x 6 y7.
For example we may want to put the legend outside of the axes which is impossible using loc best. Fortunately this is easy to do using the matplotlib pyplot legend function combined with the bbox to anchor argument. Import matplotlib pylab as plt fig ax plt subplots 1 1 figsize 10 6 make the figure with the size 10 x 6 inches fig suptitle example of a legend being placed outside of plot the data x 1 2 3 y1 1 2 4 y2 2 4 8 y3 3 5 14 labels to. The legend method adds the legend to the plot.
Often you may want to place the legend of a matplotlib plot outside of the actual plot. Reorder labels in legend. The font properties of the legend. Fontsize int or float or xx small x small small medium large x large xx large.
Put legend outside the matplotlib plot with pandas. Plt show this function is used to display the plotted figure. However sometimes we may want to have finer control over where the legend should be in the image. Prop none or matplotlib font manager fontproperties or dict.
If none default the current matplotlib rcparams will be used. Plt legend bbox to anchor 1 05 1 it places the legend at position 1 05 1 in the axes coordinate. Matplotlib has native support for legends. Legends can be placed in various positions.
Loc specifies the location of the legend bbox to anchor states the exact coordinates of the legend. The number of columns that the legend has. If you can afford to plot using pandas. Data visualization with matplotlib.