Python - Change legend size in Plotly chart
Last Updated :
23 Jun, 2022
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 to change legend size in the plotly chart.
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: Before editing size and font
Packages and CSV are imported. A plotly scatter plot is created using the px.scatter() method, X, Y, and color arguments are given. The below code is for a simple scatter plot without formatting the legend size.
To view and access the CSV file click here.
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:

After editing size and font:
Packages and CSV are imported. a plotly scatter plot is created using the px.scatter() method, X, Y, and color arguments are given. The below code is for creating a modified scatter plot where we use the update_layout() method to give extra parameters to our legend and change the font family and font size.
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")
# update_layout method used to modify change and size
fig.update_layout(legend=dict(title_font_family="Times New Roman",
font=dict(size= 20)
))
fig.show()
Output:

Similar Reads
How to change figure size in Plotly in Python In this article, we will discuss how we can change the figure size in Plotly in Python. Let's firstly take about Plotly in Python. Plotly  Python library provides us with an interactive open-source Plotly library that can support over 40 unique chart types that cover a wide list of statistical, fin
4 min read
Bar chart using Plotly in Python 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 librar
4 min read
Filled area chart using plotly in Python Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histograms, bar plots, box plots, spread plots, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization
6 min read
Change Legend Size in Base R Plot In this article, we will be looking at the approach to change the size of the legend in the plot in the R programming language. To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex grea
2 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 w
2 min read