Python Plotly – How to customize legend?

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.
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 packagesimport plotly.express as pximport pandas as pd  # importing csv filedf = pd.read_csv("iris.csv")  # scatter plot using plotlyfig = 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 packagesimport plotly.express as pximport pandas as pd  # importing csv filedf = pd.read_csv("iris.csv")  # scatter plot using plotlyfig = px.scatter(df, x="sepal_length",                  y="sepal_width",                  color="species")  fig.show()
Output:
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 packagesimport plotly.express as pximport pandas as pd  # importing csv filedf = pd.read_csv("iris.csv")  # scatter plot using plotlyfig = px.scatter(df, x="sepal_length",                 y="sepal_width",                 color="species")  # order of legend is reversedfig.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.
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 packagesimport plotly.express as pximport pandas as pd  # importing csv filedf = pd.read_csv("iris.csv")  # scatter plot using plotlyfig = px.scatter(df, x="sepal_length",                 y="sepal_width",                  color="species")  # changing orientation of the legendfig.update_layout(legend=dict(    orientation="h",  ))fig.show()
Output:
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 packagesimport plotly.express as pximport pandas as pd  # importing csv filedf = pd.read_csv("iris.csv")  # scatter plot using plotlyfig = px.scatter(df, x="sepal_length",                 y="sepal_width",                 color="species")  # adding different style parameters to the legendfig.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:
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 ?
Python Classes and Objects
Python | os.path.join() method
Introduction To PYTHON
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Create a directory in Python