Open In App
Related Articles

How to manually add a legend color and legend font size on a plotly figure in Python?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will learn how to manually add a legend color and legend font size on a plotly figure in Python. A legend is an area describing the elements of the graph. In the plotly legend is used to Place a legend on the axes.

Here we will discuss different methods for manually adding legend color and legend font size in plotly express, using two different examples for each to make it more clear.

Example 1: In this Bar chart, We are adding the legend color and legend font size using fig.update_layout() method, legend_font_color=”red” – this parameter allows you to set the fontcolor whereas legend_font_size= 19 – this parameter allows you to set the fontsize.

Python3

# importing packages
import plotly.express as px
  
# using medals_wide dataset
wide_df = px.data.medals_wide()
  
# plotting the bar chart
fig = px.bar(wide_df, x="nation", y=[
             "gold", "silver", "bronze"],
             title="Geeksforgeeks")
  
# set legend color
fig.update_layout(legend_font_color="red")
  
# set font size
fig.update_layout(legend_font_size=19)
  
# showing figure.
fig.show()

                    

Output:

Example 2: In this Scatter chart, We are adding the legend color and legend font size using fig.update_layout() method, legend_font_color=”gold” – this parameter allows you to set the fontcolor whereas legend_font_size= 10 – this parameter allows you to set the fontsize.

Python3

# importing packages
import plotly.express as px
  
# using the gapminder dataset
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex",
                 symbol="smoker", facet_col="time",
                 labels={"sex": "Gender", "smoker": "Smokes"})
  
# set legend color
fig.update_layout(legend_font_color="gold")
  
# set font size
fig.update_layout(legend_font_size=10)
  
fig.show()

                    

Output:


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 22 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials