The Wayback Machine - https://web.archive.org/web/20240709212427/https://www.geeksforgeeks.org/how-to-change-the-position-of-legend-using-plotly-python/
Open In App

How to change the position of legend using Plotly Python?

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to change the position of the legend in Plotly using Python.

The legend appears by default when variation in one object has to be depicted with reference to the other. Legend makes it easier to read a graph since it contains descriptions for the color code or keys used. By default, the legend appears on the top right corner of the plot but outside of it, but it is possible to position them as per requirement. Let us first create a regular plot so that the difference can be apparent.

Dataset Used: Click here

Example: Regular plot

Python3




# import libraries
import plotly.express as px
import pandas as pd
  
# read dataset
data = pd.read_csv("bestsellers.csv")
  
fig = px.scatter(data, x="Year", y="Price", color="Genre")
  
fig.show()


Output:

To position the legend we use the update_layout function with legend set to a dictionary that describes the attributes of a legend. anchor keys set position and x and y accommodate the margin with respect to axis.  

Syntax:

update_layout(legend=dict(yanchor, y, xanchor, x))

By setting appropriate values to the above parameters the required task can be achieved.

Example: Position the legend to the center

Python3




# import the modules
import plotly.express as px
import pandas as pd
  
# read the data
data = pd.read_csv("bestsellers.csv")
  
# scatter plot
fig = px.scatter(data, x="Year", y="Price", color="Genre")
fig.update_layout(legend=dict(y=0.5))
fig.show()


Output:

Example: Positioning legend on the plot

Python3




# import the modules
import plotly.express as px
import pandas as pd
  
# read the dataset
data = pd.read_csv("bestsellers.csv")
  
# scatter plot
fig = px.scatter(data, x="Year", y="Price", color="Genre")
# layout
fig.update_layout(legend=dict(yanchor="top", y=0.9, xanchor="left", x=0.4))
fig.show()


Output:



Similar Reads

How to manually add a legend color and legend font size on a plotly figure in Python?
In this article, we will learn how to manually add a legend color and legend font size on a plotly figure in Python. A legend is an area describing the elements of the graph. In the plotly legend is used to Place a legend on the axes. Here we will discuss different methods for manually adding legend color and legend font size in plotly express, usi
2 min read
How to hide legend with Plotly Express and Plotly in Python?
In this article, we will learn How to hide legend with Plotly Express and Plotly. Here we will discuss two different methods for hiding legend in plotly and plotly express, using two different examples for each to make it more clear. Syntax: For legend: fig.update_traces(showlegend=False)fig.update(layout_showlegend=False) Example 1: In this exampl
2 min read
Plotly - How to show legend in single-trace scatterplot with plotly express?
In this article let's see how to show legend in single-trace scatterplot with plotly express. A 'trace' is the name given to each plot inside the chart. Generally in plotly legend is not visible for single trace scatter plots. Example: In the below, example packages and data are imported and a single trace scatter plot is plotted using px.scatter()
1 min read
Python - Change legend size in Plotly chart
The data on the graph's Y-axis, also known as the graph series, is reflected in the legend of the graph. This is the information that comes from the columns of the relevant grid report, and it usually consists of metrics. A graph legend is usually displayed as a box on the right or left side of your graph. Plotly's update_layout() function is used
2 min read
Set the amount of vertical space between legend groups using Plotly-Python
In this article, we will learn how to set the amount of vertical space between legend groups. A legend is an area describing the elements of the graph. In the plotly legend is used to Place a legend on the axes. Example 1: In this Bar chart, We are setting the amount of vertical space (in px) between legend groups with the help of a method called f
1 min read
Show legend and label axes in 3D scatter plots in Python Plotly
In this article, we will learn how to show legend and label axes in 3D scatter plots in Python using the Plotly library. To create a 3D scatter plot in Plotly Express, you will need to provide the data for the x, y, and z coordinates of the points you want to plot. You can also customize the appearance of the plot, such as the size and color of the
3 min read
Hide legend entries in a plotly figure in Python
In this article, we are going to see how to hide legend entries in a plotly figure using Python. The Fig below shows the plot without hiding the legend entries: Method 1: Setting showlegend property by the name of the trace Here we are going to set showlegend property to remove the legend entries in a plotly figure. C/C++ Code import pandas as pd i
1 min read
Hide legend in plotly express in Python?
In this article, we will discuss how to hide legend in plotly express using Python. Dataset in use: bestsellers4 The legend appears by default when variation in one object has to be depicted with reference to the other. Legend makes it easier to read a graph since it contains descriptions for the color code or keys used. Creating a regular plot so
1 min read
Python Plotly - How to customize legend?
In plotly, we can customize the legend by changing order, changing orientation, we can either hide or show the legend and other modifications like increasing size, changing font and colour of the legend. In this article let's see the different ways in which we can customise the legend. To customize legend we use the update_layout() method. Syntax:
3 min read
Change the legend position in Matplotlib
In this article, we will learn how to Change the legend position in Matplotlib. Let's discuss some concepts : Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a multi-platform data visualization library built on NumPy arrays and designed to figure with the broader SciPy stack. It had been introduc
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg