Open In App

How to hide the colorbar and legend in Plotly Express?

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




# importing packages
import plotly.express as px
  
# using the gapminder dataset
data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
  
# plotting the bar chart
fig = px.scatter(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
             labels={'pop':'population of Canada'}, height=400, title="Geeksforgeeks")
  
# hiding color-bar 
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




#importing packages
import plotly.express as px
  
# using medals_wide dataset
wide_df = px.data.medals_wide()
  
# plotting the bar chart
fig = px.histogram(wide_df, x="nation", y=["gold", "silver", "bronze"], title="Geeksforgeeks")
  
# hiding legend 
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




# imports
import plotly.express as px
  
# using elections dataset
df = px.data.election()
  
# figure setup
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"
    )
  
# move colorbar
fig.update_layout(coloraxis_colorbar=dict(yanchor="top", y=1, x=0,
                                          ticks="outside",
                                          ticksuffix=" bills"))
# hiding legend
fig.update(layout_showlegend=False)
  
# hiding color-bar
fig.update(layout_coloraxis_showscale=False)
  
fig.show()


Output:

Before hiding

After Hiding



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads