Px.colors.named_colorscales() do not match px.colors.sequential.NAME

Some named color scales are capitalized like “Reds”, some not like “ice”, some mixed like “RdBu”.
when trying to iterate over color lists from all color scales, I need to use something like this:

def _get_color_scales():
    all_attributes = dir(px.colors.sequential)
    color_scales = []
    for attr in all_attributes:
        value = getattr(px.colors.sequential, attr)
        if (
                not attr.endswith("_r") and
                value and 
                isinstance(value, list) and
                isinstance(value[0], str) and
                (value[0].startswith("rgb(") or
                 value[0].startswith("#"))
        ):
            color_scales.append(attr)
    return color_scales


for color_scale in _get_color_scales():
    color_list = getattr(px.colors.sequential, color_scale)

instead of just iterating over px.colors.named_colorscales()
Is there another way of doing this slightly more elegant?

Hey @luggie, what are you after? The list of rgb, rgba or hex string for the corresponding colorscale?

If this was the case, you could make use of the swatches() function:

import plotly.express as px

fig = px.colors.sequential.swatches().to_dict()
d = dict()

for trace in fig['data']:
    colors = trace.get('marker').get('color')
    name = trace.get('y')[0]
    d[name]=colors
1 Like