✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Adding opacity to a css color in a Sankey Plot

Is it possible to add an opacity to a color if you’re using a css color? Tried doing this

            self.fig = go.Figure(data=[go.Sankey(
                    line=dict(color="black", width=0.5),
                    color=[[color, .7] for color in self.colors['link']] # self.colors is a list of css colors

but it doesn’t work and there’s no way to set an opacity flag. Any help would be appreicated.

Hi, and welcome to the community forum!

In order to modify the opacity of the color of a link in a Sankey figure, you need to pass a list of rgba colors to the figure’s link.color attribute, as demonstrated in the example at https://plotly.com/python/sankey-diagram/#more-complex-sankey-diagram-with-colored-links.

It looks like you’re on the right track but the list comprehension you perform ([color, .7] for color in self.colors['link']) does not result in a list that is in the format that the plotly.py API expects.

If you can share the result of the list comprehension it would be helpful in further debugging your issue.

Hope that helps!

Output looks like this:

[['seagreen', 0.7], ['darkgrey', 0.7], ['steelblue', 0.7], ['navy', 0.7], ['darkturquoise', 0.7]]

Is there an easy way to translate the css color to rgba?

To translate css color names to rgba programmatically, you can use the matplotlib.colors module, which has a function called to_rgba() which accepts a named color and returns an rgba value.

This is documented at https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.colors.to_rgba.html.