✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Renaming the items in colorbar

I am trying to change the colorbar values from decimal to percentage (is there any way of doing this?) Since I did not find a way, I decided to replace them manually with text, as shown in the code. It does not receive an error but the output is the same. What am I missing here?
Thanks!

import plotly.express as px
import pandas as pd

df2 = pd.read_csv(...)

fig = px.scatter_geo(df2, lon="Longitude",
                     lat="Latitude",
                     size="Pop2050",
                     color="Growth rate 2020-50",
                     color_continuous_scale=[
                         [0.0, "rgb(12,51,131)"], [0.21, "rgb(242,211,56)"], [1.0, "rgb(217,30,30)"]],
                     range_color=(-.35, 1.35),
                     color_continuous_midpoint=0,
                     projection="natural earth"
                     )

color_names = ['120%', '100%', '80%', '60%', '40%', '20%', '0%', '-20%']
color_vals = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]

fig.update_traces(marker=dict(colorbar=dict(tickmode='array',
                                            tickvals=color_vals,
                                            ticktext=color_names,
                                            lenmode='pixels')
                                            )
                         )
fig.show()

Screen Shot 2020-05-15 at 3.18.01 PM

Hi @Jubeen welcome to the forum! If you do a print(fig) you’ll see that the figure uses a coloraxis to configure the colors, and not a colorscale associated to a trace. Therefore you should call fig.update_coloraxes instead of fig.update_traces. You can take a look at the attributes of go.layout.coloraxis.ColorBar in the reference documentation, in particular there is a ticksuffix attribute if you want to display a % symbol.

1 Like