How to hide the colorbar and legend in Plotly Express?
Last Updated :
28 Nov, 2021
In this article, we will learn how to hide the colorbar and legend in plotly express.
Here we will discuss two different method for hiding color-bar and legend, using different examples to make it more clear.
Example 1:
In this example, we are hiding color-bar in Plotly Express with the help of method fig.update_coloraxes(showscale=False), by passing the showscale parameter as False.
Syntax: For color-bar:
- fig.update_coloraxes(showscale=False)
- fig.update(layout_coloraxis_showscale=False)
Python3
import plotly.express as px
data = px.data.gapminder()
data_canada = data[data.country = = 'Canada' ]
fig = px.scatter(data_canada, x = 'year' , y = 'pop' ,
hover_data = [ 'lifeExp' , 'gdpPercap' ], color = 'lifeExp' ,
labels = { 'pop' : 'population of Canada' }, height = 400 , title = "Geeksforgeeks" )
fig.update_coloraxes(showscale = False )
fig.show()
|
Output:
Before hiding
After Hiding
Example 2:
In this example, we are hiding legend in Plotly Express with the help of method fig.update_traces(showlegend=False), by passing the showlegend parameter as False.
Syntax: For legend:
- fig.update_traces(showlegend=False)
- fig.update(layout_showlegend=False)
Python3
import plotly.express as px
wide_df = px.data.medals_wide()
fig = px.histogram(wide_df, x = "nation" , y = [ "gold" , "silver" , "bronze" ], title = "Geeksforgeeks" )
fig.update_traces(showlegend = False )
fig.show()
|
Output:
Before hiding
After Hiding
Example 3:
In this Example, we are hiding legend as well as color-scale in Plotly Express at same time with the help of method fig.update(layout_showlegend=False) and fig.update(layout_coloraxis_showscale=False), by passing the parameter as False in each case.
Python3
import plotly.express as px
df = px.data.election()
fig = px.scatter_ternary(df, a = "Joly" , b = "Coderre" , c = "Bergeron" , hover_name = "district" ,
color = "total" , size = "total" , size_max = 15 , symbol = 'Coderre' ,
color_discrete_map = { "Joly" : "blue" , "Bergeron" : "green" , "Coderre" : "red" }, title = "Geeksforgeeks"
)
fig.update_layout(coloraxis_colorbar = dict (yanchor = "top" , y = 1 , x = 0 ,
ticks = "outside" ,
ticksuffix = " bills" ))
fig.update(layout_showlegend = False )
fig.update(layout_coloraxis_showscale = False )
fig.show()
|
Output:
Before hiding
After Hiding
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...