How to position legends inside a plot in Plotly-Python? - GeeksforGeeks

28 Nov, 2021
In this article, we will learn How to hide legend with Plotly Express and Plotly. 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 example, we are positioning legends inside a plot with the help of method fig.update_layout(), by passing the position as x=0.3 and y=0.1.
Python3
# importing packagesimport plotly.graph_objects as go  # using medals_wide datasetfig = go.Figure()  # plotting the scatter chartfig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[1, 2, 3, 4, 5],))  # plotting the scatter chartfig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[5, 4, 3, 2, 1],))  #  position legends inside a plotfig.update_layout(    legend=dict(        x=0.3,  # value must be between 0 to 1.        y=.1,   # value must be between 0 to 1.        traceorder="normal",        font=dict(            family="sans-serif",            size=12,            color="black"        ),    ))  fig.show()
Output:
Example 2:
In this example, we are positioning legends inside a plot with the help of method fig.update_layout(), by passing the position as x=0.6 and y=1.
Python3
# importing packagesimport plotly.graph_objects as go  # using medals_wide datasetfig = go.Figure()  # plotting the scatter chartfig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[1, 2, 3, 4, 5],))  # plotting the scatter chartfig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[5, 4, 3, 2, 1],    visible='legendonly'))  # position legends inside a plotfig.update_layout(    legend=dict(        x=0.6,  # value must be between 0 to 1.        y=1,  # value must be between 0 to 1.        traceorder="normal",        font=dict(            family="sans-serif",            size=12,            color="black"        ),    ))  fig.show()
Output:
Example 3:
In this example, we are positioning legends inside a plot with the help of method fig.update_layout(), by passing the position as x=0.9 and y=1.
Python3
import plotly.graph_objects as go  fig = go.Figure()  fig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[1, 2, 3, 4, 5],    mode='markers',    marker={'size': 10}))  # plotting the scatter chartfig.add_trace(go.Scatter(    x=[1, 2, 3, 4, 5],    y=[5, 4, 3, 2, 1],    mode='markers',    marker={'size': 100}))  # position legends inside a plotfig.update_layout(    legend=dict(        x=.9,  # value must be between 0 to 1.        y=1,   # value must be between 0 to 1.        traceorder="normal",        font=dict(            family="sans-serif",            size=12,            color="black"        ),    ))  fig.update_layout(legend={'itemsizing': 'constant'})  fig.show()
Output:
Picked
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()