Use a seaborn color palette for plotly figures

If you choose carefully a seaborn color palette, such as:

palette = sns.color_palette("Spectral", n_colors=n_clusters)

and you want to use it in a plotly figure, it’s not as straightforward as it should. You need first to scale it and convert it first:

palette_for_plotly = [f"rgb({c[0]*256}, {c[1]*256}, {c[2]*256})" for c in palette]

So you can have consistent colours scheme across all of your figures ✨

fig = ff.create_choropleth(
    fips=labels.fips, values=labels.label,
    show_state_data=True,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='County cluster',
    legend_title='cluster',
    colorscale=palette_for_plotly,
)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *