Python Plotly - How to customize legend?
Last Updated :
04 Jan, 2022
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: update_layout(dict1=None, overwrite=False, **kwargs)
The values in the input dict / keyword arguments are used to iteratively alter the parts of the original layout.
Parameters:
- dict1 (dict) – To be updated is a dictionary of properties.
- overwrite (bool) – If True, existing properties will be overwritten. If False, recursively apply updates to existing properties, retaining properties that are not specified in the update operation.
- kwargs – To be updated is a keyword/value pair of properties.
Example 1: Showing and hiding legend
Hiding legend: In the below code we import plotly.express package and pandas package. CSV file is imported, a scatterplot is displayed, the plot is further modified by the update_layout() method and the parameter showlegend is set to False.
To access the CSV file click iris
Python3
#import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# initializing showlegend to "False"
fig.update_layout(showlegend=False)
fig.show()
Output: The legend is not displayed.
By default showlegend Parameter is true. When we draw a plot in plotly legend is always displayed.
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
fig.show()
Output:

Example 2: Changing order of legend
In the below code we introduce a new parameter, legend_traceorder, and initialize is to "reversed", the order of the legend is reversed by doing so.
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# order of legend is reversed
fig.update_layout(legend_traceorder="reversed")
fig.show()
Output:
Before changing order:
After changing order:
The order setosa, versicolor , verginica is changed to virginica, versicolour , setosa.
Example 3: Changing orientation of the legend
For a horizontal legend, set the layout.legend.orientation attribute to "h." We also put it over the plotting area here. Generally, the legend is displayed vertically.
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# changing orientation of the legend
fig.update_layout(legend=dict(
orientation="h",
))
fig.show()
Output:

Example 4: Changing size, font, and color of legend
In this example, many other parameters are introduced, such as title_font_family, font where a dictionary of sub-parameters are specified for styling, bgcolor which is background colour, border colour and border width.
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# adding different style parameters to the legend
fig.update_layout(
legend=dict(
x=0,
y=1,
title_font_family="Times New Roman",
font=dict(
family="Courier",
size=12,
color="black"
),
bgcolor="LightBlue",
bordercolor="Black",
borderwidth=1
)
)
fig.show()
Output:
Similar Reads
How to Customize the Modebar in Plotly Using R? Plotly is a library for creating interactive graphs and visualizations. The modebar provides tools for zooming, downloading, and resetting the graph. Customizing the modebar can improve usability and make visualizations more suited to your needs. In this article, we'll explore how to customize the m
3 min read
How to Hide Legend from Seaborn Pairplot Seaborn is a powerful Python library for data visualization, built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. One common task when creating visualizations is to manage the legend, which can sometimes clutter the plot or distr
4 min read
Python Plotly - How to set up a color palette? In Plotly a color palette is a set of colors that you can use to make your plots look more attractive. You can set up a color palette to control how the colors in your graph. In this article, we will discuss how to set up a color palette in plotly. Method 1: Setting up the color palette for continuo
3 min read
How to group Bar Charts in Python-Plotly? Plotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 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
2 min read