Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Plotly – How to set colorbar position for a choropleth map?

  • Last Updated : 05 Nov, 2021

In this article, we will learn how to set colorbar position for a choropleth map in Python using Plotly.

Color bar are gradients that go from bright to dark or the other way round. They are great for visualizing data that go from low to high, like income, temperature, or age. Choropleth maps are used to plot maps with shaded or patterned areas which are colored, shaded or patterned in relation to a data variable. They are composed of colored polygons. They are used for representing spatial variations of a quantity over a geographical area.

Here we will discuss, how to set colorbar position for a choropleth map using different examples to make it more clear.

Syntax: # set colorbar position  

  • fig.update_layout(coloraxis_colorbar_x = float value)
  • fig.update_layout(coloraxis_colorbar_y = float value)

Example 1: Set colorbar position for X-axis

Python3




# importing libraries
import plotly.express as px
  
# figure setup using dataset
fig = px.choropleth(locations=["CA", "TX", "NY"],
                    locationmode="USA-states",
                    color=[1, 2, 3], scope="usa",
                    title="Geeksforgeeks")
  
# set colorbar position for X-axis
fig.update_layout(coloraxis_colorbar_x=0.26)
  
fig.show()

 Output:

Example 2: Set colorbar position for Y-axis

Python3




# importing packages
import plotly.express as px
  
# using gapminder datasheet.
df = px.data.gapminder().query("year==2007")
  
# figure setup
fig = px.choropleth(df, locations="iso_alpha",
                      
                    # lifeExp is a column of gapminder
                    color="lifeExp",  
                      
                    # column to add to hover information
                    hover_name="country",  
                    color_continuous_scale=px.colors.sequential.Plasma)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=-0.3)
  
fig.show()

Output:

Example 3: Set color-bar position for X-axis and Y-axis at the same time

Here we can also see that this method is also applicable to another graph too.

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")
  
# set colorbar position for X-axis
fig.update_layout(coloraxis_colorbar_x=0.9)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=0.1)
  
fig.show()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!