Set the amount of vertical space between legend groups using Plotly-Python
Last Updated :
22 Nov, 2021
In this article, we will learn how to set the amount of vertical space between legend groups.
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 Bar chart, We are setting the amount of vertical space (in px) between legend groups with the help of a method called fig.update_layout(legend_tracegroupgap= 2), by passing the legend_tracegroupgap parameter as 2 pixels.
Python3
import plotly.express as px
wide_df = px.data.medals_wide()
fig = px.bar(wide_df, x = "nation" , y = [
"gold" , "silver" , "bronze" ],
title = "Geeksforgeeks" )
fig.update_layout(legend_tracegroupgap = 2 )
fig.show()
|
Output:
Example 2: In this Scatter chart, We are setting the amount of vertical space (in px) between legend groups with the help of a method called fig.update_layout(legend_tracegroupgap= 2), by passing the legend_tracegroupgap parameter as 12 pixels.
Python3
import plotly.express as px
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" })
fig.update_layout(legend_tracegroupgap = 12 )
fig.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...