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
import plotly.express as px
fig = px.choropleth(locations = [ "CA" , "TX" , "NY" ],
locationmode = "USA-states" ,
color = [ 1 , 2 , 3 ], scope = "usa" ,
title = "Geeksforgeeks" )
fig.update_layout(coloraxis_colorbar_x = 0.26 )
fig.show()
|
Output:
Example 2: Set colorbar position for Y-axis
Python3
import plotly.express as px
df = px.data.gapminder().query( "year==2007" )
fig = px.choropleth(df, locations = "iso_alpha" ,
color = "lifeExp" ,
hover_name = "country" ,
color_continuous_scale = px.colors.sequential.Plasma)
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
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_layout(coloraxis_colorbar_x = 0.9 )
fig.update_layout(coloraxis_colorbar_y = 0.1 )
fig.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...