Colorbar minimum and maximum

Question:

How can I manually change the minimum and maximum value of a colorbar in plotly?

For example, how can I set the minimum of the colorbar in the following plot to 0?

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale='RdBu',
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

enter image description here

Asked By: Stücke

||

Answers:

You can pass range_color=[min, max] to px.treemap() :

range_color (list of two numbers) – If provided, overrides
auto-scaling on the continuous color scale.

For example you can pass arbitrary values :

range_color=[0, 100]

or set a range that fits the dataframe min and max values :

range_color=[df['lifeExp'].min(), df['lifeExp'].max()]

Also, you migth want to use the median as color midpoint instead of the average to better illustrate ‘lifeExp’ discrepancies between countries.

Answered By: EricLavault
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.