Skip to content

Instantly share code, notes, and snippets.

@ColdTeapot273K
Created January 31, 2022 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColdTeapot273K/5d8a96a80029f5cb0d6a4c57ffb70c7d to your computer and use it in GitHub Desktop.
Save ColdTeapot273K/5d8a96a80029f5cb0d6a4c57ffb70c7d to your computer and use it in GitHub Desktop.
Transform Plotly colorscale into something dark-theme-friendly
import plotly.express as px
palette = px.colors.sequential.Bluered.copy()
new_palette = list()
for color in palette:
orig_color = color[4:-1].split(",")
# offset = 50
# offset = 75
offset = 100
orig_color = [np.clip(offset + int(color_elem), 0, 255) for color_elem in orig_color]
new_color = f"rgb{orig_color[0], orig_color[1], orig_color[2]}"
new_palette.append(new_color)
# ---
# Testing:
# Vanilla
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="size",
title="Numeric 'size' values mean continuous color", color_continuous_scale=palette)
fig.show()
# New
fig = px.scatter(df, x="total_bill", y="tip", color="size",
title="Numeric 'size' values mean continuous color", color_continuous_scale=new_palette)
fig.show()
@ColdTeapot273K
Copy link
Author

Old:
image

New:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment