How to change the position of legend using Plotly Python?


Plotly python is the library in python to create interactive visualizations using the plotly java script. It provides a wide range of charts and plots like scatter plots, line charts, bar charts and so on.

Installing plotly library

To use the plotly python library first we have to install the library in our python environment. The below is the code to install plotly python.

pip install plotly

On successful installation the above command generates the following output –

Defaulting to user installation because normal site-packages is not writeable
Collecting plotly
  Downloading plotly-5.14.1-py2.py3-none-any.whl (15.3 MB)
     ---------------------------------------- 15.3/15.3 MB 3.2 MB/s eta 0:00:00
Collecting tenacity>=6.2.0 (from plotly)
  Downloading tenacity-8.2.2-py3-none-any.whl (24 kB)
Installing collected packages: tenacity, plotly
Successfully installed plotly-5.14.1 tenacity-8.2.2

Changing the position of the legend in plotly

The legend is used to add the context and clarity to the data, which displays the names of each data series in our chart and the corresponding colors too. We can change the position of the legend in plotly using the legend property.

Syntax

The syntax for changing the position of the legend in plotly python is as follows.

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

Where, yanchor and xanchor which specifies the x-axis and y-axis.

Example

In the following example, we pass the dataset link to the scatter() function of plotly to create a scatter plot for the given coloumns. We then update the chart to display the legend at the top right of the chart.

import plotly.express as px
import pandas as pd
data = pd.read_csv("https://raw.githubusercontent.com/Opensourcefordatascience/Data-sets/master/blood_pressure.csv")
fig = px.scatter(data, x="agegrp", y="bp_before", color="sex")
fig.update_layout(legend=dict(yanchor="top", y=0.9, xanchor="right", x=0.4))
fig.show()

Output

When we run the above code, following output will be generated -

Example

Let’s see another example for changing the legend position using the plotly python library.

import plotly.express as px
import pandas as pd
data = {"age" : [20,23,24,30,20],"year" : [2000,2004,2006,2009,2018]}
data = pd.DataFrame(data)
fig = px.scatter(data, x = "age", y = "year",color = "age")
fig.update_layout(legend = dict(yanchor="top", y=0.3, xanchor="right", x=0.4))
fig.show()

Output

Example

In this example we are creating a bar chart and setting the position of the legend on the bottom left position of the chart.

import plotly.graph_objects as px
import pandas as pd

x = ['1-10 Overs', '11-20 Overs', '21-30 Overs', '31-40 Overs']

plot = px.Figure(data=[px.Bar(
    name = 'Player1',
    x = x,
    y = [12,30,40,20]
   ),px.Bar(
       name = 'Player2',
       x = x,
       y = [20,25,30,30]
   )
])
     
plot.update_layout(legend = dict(yanchor="top", y=0, xanchor="left", x=0))
plot.show()

Output

Following is the output of the legend position on the chart.

Updated on: 09-Aug-2023

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements