04-22-2023, 11:08 PM
Python ? Put Legend Outside Plot ? – Easy Guide
5/5 – (1 vote)
Are you tired of feeling boxed in by your Python plots and ready to break free from the constraints of traditional legend placement?
In this guide, I’ll show you how to put legends outside your plot for (click to
jump):
Let’s start with the first!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
![[Image: 1f469-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469-200d-1f4bb.png)
Matplotlib Put Legend Outside Plot
Let’s start with various ways to position the legend outside for better visualization and presentation.
Matplotlib Set Legend Outside Plot (General)
First, let’s adjust the legend’s position outside the plot in general. To do this, use the parameter in the legend function like this:
. Here, adjust the values of and to control the legend’s position.
![[Image: image-220.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-220.png)
In this example, the parameter is set to , which moves the legend slightly to the right of the plot.
Info: The parameter in uses a tuple of two values, and , to control the position of the legend. controls the left/right and controls the bottom/top legend placement.
Generally, in axes coordinates:
However, the values of and are not limited to the range . You can use values outside of this range to place the legend beyond the axes’ boundaries.
For example:
Using negative values is also allowed. For example:
The range of and depends on the desired position of the legend relative to the plot. By adjusting these values, you can fine-tune the legend’s position to create the perfect visualization. ![[Image: 1f4ab.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ab.png)
Matplotlib Set Legend Below or Above Plot
To place the legend below the plot, you can set the parameter as ‘upper center’ and use like this: . For placing the legend above the plot, use instead
.
![[Image: image-221.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-221.png)
Matplotlib Set Legend Left of Plot (Upper, Center, Lower Left)
For positioning the legend to the left of the plot, use the following examples:
Matplotlib Set Legend Right of Plot (Upper, Center, Lower Right)
To position the legend to the right of the plot, you can try the following:
![[Image: image-222-1024x447.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-222-1024x447.png)
Matplotlib Set Subplots Legend Outside Plot
When working with subplots, you can place a single, unified legend outside the plot by iterating over the axes and using the method on the last axis (source)
. Remember to use the parameter to control the legend’s position.
Here’s an example that does two things, i.e., (1) placing the legend on the right and (2) adjusting the layout to accommodate the external legend:
![[Image: image-223.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-223.png)
Legend Outside Plot Is Cut Off
If your legend is cut off, you can adjust the saved figure’s dimensions using. The parameter with the value ‘tight’ will ensure that the whole legend is visible on the saved figure
.
Add Legend Outside a Scatter Plot
For a scatter plot, you can use the same approach as mentioned earlier by adding the and parameters to position the legend outside the plot. For instance, will place the legend in the upper right corner outside the scatter plot
.
If your legend is cut off when placing it outside the plot in Python’s Matplotlib, you can adjust the layout and save or display the entire figure, including the external legend, by following these steps:
Here’s an example demonstrating these steps:
![[Image: image-224-1024x462.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-224-1024x462.png)
In this example, you use the function to adjust the layout of the figure and make room for the legend.
You can also use the function, which automatically adjusts the layout based on the elements in the figure:
![[Image: image-225-1024x508.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-225-1024x508.png)
In this case, the parameter is a list , which specifies the normalized figure coordinates of the new bounding box for the subplots. Adjust the values in the parameter as needed to ensure the legend is not cut off.
Additional Legend Configurations
In this section, you’ll learn about some additional ways to customize the legend for your plots in Python using Matplotlib. This will help you create more meaningful and visually appealing visualizations. However, feel free to skip ahead to the following sections on plotting the legend outside the figure in Seaborn, Pandas, and Bokeh.
Python Set Legend Position
As you already know by now, you can place the legend at a specific position in the plot by using the parameter. For example, you could place the legend outside the plot to the right by passing as the argument:
This will place the legend slightly outside the right border of the plot, with its vertical center aligned with the plot center.
Python Set Legend Location
You can easily change the location of the legend by using the parameter. Matplotlib allows you to use predefined locations like , , etc., or use a specific coordinate by passing a tuple:
This will place the legend in the upper-left corner of the plot.![[Image: 1f4cd.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4cd.png)
Python Set Legend Font Size
To change the font size of the legend, you can use the parameter. You can pass a numeric value or a string like , , or to set the font size:
This will increase the font size of the legend text.![[Image: 1f603.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f603.png)
Python Set Legend Title
If you want to add a title to your legend, you can use the parameter. Just pass a string as the argument to set the title:
This will add the title “My Legend Title” above your legend.![[Image: 1f44d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f44d.png)
Python Set Legend Labels
If you’d like to customize the legend labels, you can pass the parameter. It takes a list of strings as the argument:
Your legend will now display the custom labels “Label 1” and “Label 2” for the corresponding plot elements.![[Image: 1f3f7.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3f7.png)
Python Set Legend Color
Changing the color of the legend text and lines can be achieved by using the parameter. Just pass a color string or a list of colors:
This will change the color of the legend text and lines to red.![[Image: 1f534.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f534.png)
Seaborn Put Legend Outside Plot
In this section, I’ll show you how to move the legend outside the plot using Seaborn. Let’s dive into various ways of setting the legend position, like below, above, left or right of the plot.![[Image: 1f468-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f468-200d-1f4bb.png)
Sns Set Legend Outside Plot (General)
First, let’s talk about the general approach:
You can use the function and the parameter from Matplotlib to move the legend. You can combine this with the parameter to fine-tune the legend’s position.![[Image: 1f4c8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4c8.png)
Here’s a quick example:
![[Image: image-226.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-226.png)
Sns Set Legend Below or Above Plot
Now let’s move the legend below or above the plot.
Simply adjust the parameter accordingly. For example, to place the legend below the plot, you can use :
And to place the legend above the plot, use:
Sns Set Legend Left of Plot (Upper, Center, Lower Left)
Similarly, to position the legend on the left side of the plot, you can use the following code snippets:![[Image: 1f3a8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3a8.png)
Upper left:
Center left:
Lower left:
Sns Set Legend Right of Plot (Upper, Center, Lower Right)
Lastly, to place the legend on the right side of the plot, adjust the parameter like so:![[Image: 1f680.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png)
Upper right:
Center right:
Lower right:
With these techniques, you can easily position the legend outside the plot using Seaborn! Happy plotting!![[Image: 1f469-200d-1f52c.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469-200d-1f52c.png)
Pandas Put Legend Outside Plot
When working with Pandas and Matplotlib, you often want to move the legend outside the plot
to improve readability. No worries! You can do this by taking advantage of the fact that returns a Matplotlib axis, enabling you to add to your code.
Here’s how:
![[Image: image-227.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-227.png)
First, import the necessary libraries like Pandas and Matplotlib:
Create your DataFrame and plot it using the function, like this:
Next, you’ll want to place the legend outside the plot. Adjust the coordinates parameter,, to position it according to your preferences. For example, if you want to place the legend to the right of the plot, use:
This code will place the legend to the right of the plot, at the top
.
Bokeh Put Legend Outside Plot
Placing a legend outside the plot in Bokeh can be easily done by using the method. You’ll need to create a object manually and then add it to your plot using . This gives you the flexibility to position the legend anywhere on your plot, which is particularly helpful when you have many curves that might be obscured by an overlapping legend. ![[Image: 1f60a.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f60a.png)
Here’s a short example to help you move the legend outside the plot in Bokeh:
![[Image: image-228.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-228.png)
To ensure your plot is as clear as possible, we recommend experimenting with different legend positions and layouts. By customizing your plot, you can maintain a clean and organized display of your curves even when there’s a lot of information to convey.
With Bokeh, you have full control over your plot’s appearance, so enjoy exploring different options to find the perfect fit for your data!![[Image: 1f4c8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4c8.png)
![[Image: 1f389.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f389.png)
Keep Learning With Your Python Cheat Sheet!![[Image: 2705.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2705.png)
Feel free to download our free cheat sheet and join our tech and coding academy by downloading the free Finxter cheat sheets — it’s fun!
5/5 – (1 vote)
Are you tired of feeling boxed in by your Python plots and ready to break free from the constraints of traditional legend placement?
In this guide, I’ll show you how to put legends outside your plot for (click to
jump):Let’s start with the first!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
![[Image: 1f469-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469-200d-1f4bb.png)
Matplotlib Put Legend Outside Plot
Let’s start with various ways to position the legend outside for better visualization and presentation.
Matplotlib Set Legend Outside Plot (General)
First, let’s adjust the legend’s position outside the plot in general. To do this, use the
Code:
bbox_to_anchorCode:
matplotlib.pyplot.legend(bbox_to_anchor=(x, y))
. Here, adjust the values of Code:
xCode:
y![[Image: image-220.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-220.png)
Code:
import matplotlib.pyplot as plt # Sample data for plotting
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125] # Create the plot
plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3') # Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1)) # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Display the plot
plt.show()In this example, the
Code:
bbox_to_anchorCode:
(1.05, 1)
Info: The Code:
bbox_to_anchorCode:
matplotlib.pyplot.legend()Code:
xCode:
yCode:
x=0/1Code:
y=0/1Generally, in axes coordinates:
- represents the left-bottom corner of the axes.Code:
(0, 0)
- represents the left-top corner of the axes.Code:
(0, 1)
- represents the right-bottom corner of the axes.Code:
(1, 0)
- represents the right-top corner of the axes.Code:
(1, 1)
However, the values of
Code:
xCode:
yCode:
[0, 1]For example:
- (1.05, 1) places the legend slightly to the right of the top-right corner of the axes.
- (0, 1.1) places the legend slightly above the top-left corner of the axes.
Using negative values is also allowed. For example:
- (-0.3, 0) places the legend to the left of the bottom-left corner of the axes.
- (1, -0.2) places the legend below the bottom-right corner of the axes.
The range of
Code:
xCode:
y![[Image: 1f4ab.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ab.png)
Matplotlib Set Legend Below or Above Plot
To place the legend below the plot, you can set the
Code:
locCode:
bbox_to_anchorCode:
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1))Code:
bbox_to_anchor=(0.5, 1.1)
.![[Image: image-221.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-221.png)
Matplotlib Set Legend Left of Plot (Upper, Center, Lower Left)
For positioning the legend to the left of the plot, use the following examples:
- Upper left: Code:
plt.legend(loc='center left', bbox_to_anchor=(-0.2, 0.5))![[Image: 1f31f.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f31f.png)
- Center left: Code:
plt.legend(loc='center left', bbox_to_anchor=(-0.1, 0.5))
- Lower left: Code:
plt.legend(loc='lower left', bbox_to_anchor=(-0.2, 0))
Matplotlib Set Legend Right of Plot (Upper, Center, Lower Right)
To position the legend to the right of the plot, you can try the following:
- Upper right: Code:
plt.legend(loc='upper right', bbox_to_anchor=(1.1, 1))![[Image: 1f44d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f44d.png)
- Center right: Code:
plt.legend(loc='center right', bbox_to_anchor=(1.1, 0.5))
- Lower right: Code:
plt.legend(loc='lower right', bbox_to_anchor=(1.1, 0))
![[Image: image-222-1024x447.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-222-1024x447.png)
Matplotlib Set Subplots Legend Outside Plot
When working with subplots, you can place a single, unified legend outside the plot by iterating over the axes and using the
Code:
legend()
. Remember to use the Code:
bbox_to_anchorHere’s an example that does two things, i.e., (1) placing the legend on the right and (2) adjusting the layout to accommodate the external legend:
![[Image: image-223.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-223.png)
Code:
import numpy as np
import matplotlib.pyplot as plt # Sample data for plotting
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x) # Create subplots
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True) # Plot data on the first subplot
axes[0].plot(x, y1, label='sin(x)')
axes[0].set_title('Sine Wave') # Plot data on the second subplot
axes[1].plot(x, y2, label='cos(x)', color='orange')
axes[1].set_title('Cosine Wave') # Set a common x-label for both subplots
fig.text(0.5, 0.04, 'x', ha='center') # Set y-labels for individual subplots
axes[0].set_ylabel('sin(x)')
axes[1].set_ylabel('cos(x)') # Create a unified legend for both subplots
handles, labels = axes[-1].get_legend_handles_labels()
for ax in axes[:-1]: h, l = ax.get_legend_handles_labels() handles += h labels += l # Place the unified legend outside the plot using bbox_to_anchor
fig.legend(handles, labels, loc='upper right', bbox_to_anchor=(1, 0.75)) # Adjust the layout to accommodate the external legend
fig.subplots_adjust(right=0.7) # Display the subplots
plt.show()If your legend is cut off, you can adjust the saved figure’s dimensions using
Code:
plt.savefig('filename.ext', bbox_inches='tight')Code:
bbox_inches
.Add Legend Outside a Scatter Plot
For a scatter plot, you can use the same approach as mentioned earlier by adding the
Code:
locCode:
bbox_to_anchorCode:
plt.legend(loc='upper right', bbox_to_anchor=(1.1, 1))
.If your legend is cut off when placing it outside the plot in Python’s Matplotlib, you can adjust the layout and save or display the entire figure, including the external legend, by following these steps:
- Use the parameter in theCode:
bbox_to_anchorfunction to control the position of the legend.Code:legend()
- Adjust the layout of the figure using the orCode:
subplots_adjust()function to make room for the legend.Code:tight_layout()
- Save or display the entire figure, including the external legend.
Here’s an example demonstrating these steps:
![[Image: image-224-1024x462.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-224-1024x462.png)
Code:
import matplotlib.pyplot as plt # Sample data for plotting
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125] # Create the plot
plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3') # Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Adjust the layout to accommodate the external legend
plt.subplots_adjust(right=0.7) # Display the plot
plt.show()In this example, you use the
Code:
subplots_adjust()You can also use the
Code:
tight_layout()![[Image: image-225-1024x508.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-225-1024x508.png)
Code:
# ...
# Set the legend's position outside the plot using bbox_to_anchor
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') # Add axis labels and title
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plotting with External Legend') # Automatically adjust the layout to accommodate the external legend
plt.tight_layout(rect=[0, 0, 0.7, 1]) # Display the plot
plt.show()In this case, the
Code:
rectCode:
[left, bottom, right, top]Code:
rectAdditional Legend Configurations
In this section, you’ll learn about some additional ways to customize the legend for your plots in Python using Matplotlib. This will help you create more meaningful and visually appealing visualizations. However, feel free to skip ahead to the following sections on plotting the legend outside the figure in Seaborn, Pandas, and Bokeh.
Python Set Legend Position
As you already know by now, you can place the legend at a specific position in the plot by using the
Code:
bbox_to_anchorCode:
(1.05, 0.5)Code:
import matplotlib.pyplot as plt
plt.legend(bbox_to_anchor=(1.05, 0.5))This will place the legend slightly outside the right border of the plot, with its vertical center aligned with the plot center.
Python Set Legend Location
You can easily change the location of the legend by using the
Code:
locCode:
'upper right'Code:
'lower left'Code:
plt.legend(loc='upper left')This will place the legend in the upper-left corner of the plot.
![[Image: 1f4cd.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4cd.png)
Python Set Legend Font Size
To change the font size of the legend, you can use the
Code:
fontsizeCode:
'small'Code:
'medium'Code:
'large'Code:
plt.legend(fontsize='large')This will increase the font size of the legend text.
![[Image: 1f603.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f603.png)
Python Set Legend Title
If you want to add a title to your legend, you can use the
Code:
titleCode:
plt.legend(title='My Legend Title')This will add the title “My Legend Title” above your legend.
![[Image: 1f44d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f44d.png)
Python Set Legend Labels
If you’d like to customize the legend labels, you can pass the
Code:
labelsCode:
plt.legend(labels=['Label 1', 'Label 2'])Your legend will now display the custom labels “Label 1” and “Label 2” for the corresponding plot elements.
![[Image: 1f3f7.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3f7.png)
Python Set Legend Color
Changing the color of the legend text and lines can be achieved by using the
Code:
labelcolorCode:
plt.legend(labelcolor='red')This will change the color of the legend text and lines to red.
![[Image: 1f534.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f534.png)
Seaborn Put Legend Outside Plot
In this section, I’ll show you how to move the legend outside the plot using Seaborn. Let’s dive into various ways of setting the legend position, like below, above, left or right of the plot.
![[Image: 1f468-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f468-200d-1f4bb.png)
Sns Set Legend Outside Plot (General)
First, let’s talk about the general approach:
You can use the
Code:
legend()Code:
bbox_to_anchorCode:
loc![[Image: 1f4c8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4c8.png)
Here’s a quick example:
![[Image: image-226.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-226.png)
Code:
import seaborn as sns
import matplotlib.pyplot as plt # Sample data for plotting
tips = sns.load_dataset("tips") # Create a Seaborn plot with axis variable 'ax'
fig, ax = plt.subplots()
sns_plot = sns.scatterplot(x="total_bill", y="tip", hue="day", data=tips, ax=ax) # Set the legend's position outside the plot using bbox_to_anchor on 'ax'
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.) # Add axis labels and title
ax.set_xlabel('Total Bill')
ax.set_ylabel('Tip')
ax.set_title('Tips by Total Bill and Day') # Display the plot
plt.show()Now let’s move the legend below or above the plot.
Simply adjust the Code:
bbox_to_anchorCode:
bbox_to_anchor=(0.5, -0.1)Code:
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1))And to place the legend above the plot, use
Code:
bbox_to_anchor=(0.5, 1.1)Code:
ax.legend(loc='lower center', bbox_to_anchor=(0.5, 1.1))Similarly, to position the legend on the left side of the plot, you can use the following code snippets:
![[Image: 1f3a8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f3a8.png)
Upper left:
Code:
ax.legend(loc='upper right', bbox_to_anchor=(-0.15, 1))Center left:
Code:
ax.legend(loc='center right', bbox_to_anchor=(-0.15, 0.5))Lower left:
Code:
ax.legend(loc='lower right', bbox_to_anchor=(-0.15, 0))Lastly, to place the legend on the right side of the plot, adjust the
Code:
bbox_to_anchor![[Image: 1f680.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png)
Upper right:
Code:
ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1))Center right:
Code:
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5))Lower right:
Code:
ax.legend(loc='lower left', bbox_to_anchor=(1.05, 0))With these techniques, you can easily position the legend outside the plot using Seaborn! Happy plotting!
![[Image: 1f469-200d-1f52c.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f469-200d-1f52c.png)
Pandas Put Legend Outside Plot
When working with Pandas and Matplotlib, you often want to move the legend outside the plot
to improve readability. No worries! You can do this by taking advantage of the fact that Code:
.plot()Code:
.legend(bbox_to_anchor=(x, y))Here’s how:
![[Image: image-227.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-227.png)
First, import the necessary libraries like Pandas and Matplotlib:
Code:
import pandas as pd
import matplotlib.pyplot as pltCreate your DataFrame and plot it using the
Code:
plot()Code:
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
ax = df.plot()Next, you’ll want to place the legend outside the plot. Adjust the coordinates parameter,
Code:
bbox_to_anchorCode:
ax.legend(bbox_to_anchor=(1.05, 1)) plt.tight_layout()
plt.show()This code will place the legend to the right of the plot, at the top
.Bokeh Put Legend Outside Plot
Placing a legend outside the plot in Bokeh can be easily done by using the
Code:
add_layoutCode:
LegendCode:
add_layout![[Image: 1f60a.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f60a.png)
Here’s a short example to help you move the legend outside the plot in Bokeh:
![[Image: image-228.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-228.png)
Code:
from bokeh.plotting import figure, show
from bokeh.models import Legend, LegendItem
from bokeh.io import output_notebook
output_notebook() # Example data
x = list(range(10))
y1 = [i**2 for i in x]
y2 = [i**0.5 for i in x] # Create a figure
p = figure(title="Example Plot") # Add line glyphs and store their renderer
r1 = p.line(x, y1, line_color="blue", legend_label="Line 1")
r2 = p.line(x, y2, line_color="red", legend_label="Line 2") # Create Legend object
legend = Legend(items=[ LegendItem(label="Line 1", renderers=[r1]), LegendItem(label="Line 2", renderers=[r2])
], location="top_left") # Add legend to plot
p.add_layout(legend, 'right') # Show plot
show(p)To ensure your plot is as clear as possible, we recommend experimenting with different legend positions and layouts. By customizing your plot, you can maintain a clean and organized display of your curves even when there’s a lot of information to convey.
With Bokeh, you have full control over your plot’s appearance, so enjoy exploring different options to find the perfect fit for your data!
![[Image: 1f4c8.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f4c8.png)
![[Image: 1f389.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f389.png)
Keep Learning With Your Python Cheat Sheet!
![[Image: 2705.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2705.png)
Feel free to download our free cheat sheet and join our tech and coding academy by downloading the free Finxter cheat sheets — it’s fun!

