# Place legend outside of the plot in matplotlib

In this tutorial, we are going to learn how can we **Place legend outside of the plot in matplotlib**. For this let’s understand some basic concepts of *matplotlib*.

**Matplotlib** is a library in python which is used to create interactive visualization. You can create plots, charts, etc. It gives us a better understanding of the data.

**Legend** in the plot is the area where the data elements of the graph are shown. For this, there is a * legend()* function in matplotlib. We can customize the legend, we can give which elements to show in the legend. If any parameter is not provided legend will automatically take all elements present in Graph or Plot.

## Placing legend in the plot in matplotlib

Let’s take one example for a better understanding. Here firstly we are importing **matplotlib as plt** and **NumPy as np** to our system. Later we are adding the function of NumPy it divides the plot range into evenly distributed ranges.

`linespace()`

#importing libaries import matplotlib.pyplot as plt import numpy as np #deciding linespace x = np.linspace(0, 7) #plotting data plt.plot(x, np.sin(x), label="sin(x)") plt.plot(x, np.cos(x), label="cos(x)") # placing legend outside plot plt.legend(bbox_to_anchor=(1.05, 1.0), loc='lower left') # showing plot plt.show()

As you can see in the above code for plotting sine and cosine function we are using **np.sin** of NumPy and plot function of matplotlib.

For placing legend in the plot we are using function. And by using the

`plt.legend()`

*function we can decide the position co-ordinate of legend. So for placing legend outside of a plot, Here we are giving parameters as*

**bbox_to_anchor()***(1.05, 1.0)*so legend will be placed at axes coordinate

*(1.05, 1.0)*.

Where

*(0, 0)*is co-ordinate of the lower-left corner, and

*(1.0, 1.0)*is the upper-right corner of the co-ordinate axes with respect to the plot.

As you can see output legend is placed in the upper right corner. Using we are retrieving the output plot.

`plt.show()`

## Placing legend outside the plot in matplotlib

Let’s take another example where the legend is placed at the middle right of the plot.

#plotting data plt.plot(x, np.cos(x), label="cos(x)") plt.plot(x, np.sin(x), label="sin(x)") # placing legend outside plot plt.legend(bbox_to_anchor=(1.05, 0.6), loc='upper left') # showing plot plt.show()

Here we are adding loc parameter to * bbox_to_anchor()* function. The

**loc**is the position from which the legend position is decided. For understanding, it clearly let’s see in the example above.

Here **loc=’upper left’** is given means the upper left corner of the legend is at the position of *(1.05, 0.6)* axes co-ordinate.

Output

## Plotting graph with legend using tight_plot

Here is another example. Now we are plotting the tangent function, and placing legend on the upper side of the plot.

#deciding linespace x = np.linspace(0, 10,100) #plotting tangent graph plt.plot(x, np.tan(x), label="tan(x)") #placing legend at upper side of graph plt.legend(bbox_to_anchor=(0.4, 1.0), loc='lower left') #adjusting padding around plot plt.tight_layout() #showing plot plt.show()

As you can see in the above code we are increasing linespace. After that, we are plotting the tan function graph. We are giving the position of legend to the function* bbox_to_anchor*. Here reference co-ordinate point is the lower-left corner of legend.

For adjusting padding of our graph/plot we are using ** plt.tight_layout()** function. This function will return the best fit of our plot.

Output

## Leave a Reply