Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython Plotly – How to set colorbar position for a choropleth map?

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

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="GeeksforLazyroar")
  
# 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="GeeksforLazyroar")
  
# 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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments