Adjusting density heatmap so it can overlay through an image

I have a code similar like:
import plotly.express as px
df = px.data.tips()

fig = px.density_heatmap(df, x=“total_bill”, y=“tip”, facet_row=“sex”, facet_col=“smoker”)
fig.show()

I saw on parameters of density heatmap that they have an opacity parameter. I need it so it can have an overlay on an image. But when I use the parameter it gives an error that opacity doesn’t exist.

Any suggestions on how to solve this? Thanks in advance

Hi @phate ,

you are right about the error, I get the same. Maybe a workaround like this could do the trick:

def change_color(plotly_color, opacity=1.0):
    # set empty list
    chgd_plotly_color=[]

    # loop over colors in list
    for color in plotly_color:
        color_string = color.replace('rgb', 'rgba')
        color_string = color_string.replace(')',f', {str(opacity)})')
        chgd_plotly_color.append(color_string)
    return chgd_plotly_color

and then using your example:

import plotly.express as px
df = px.data.tips()

fig = px.density_heatmap(df, x='total_bill', y='tip', facet_row='sex', facet_col='smoker', color_continuous_scale=change_color(px.colors.sequential.thermal, opacity=0.1))
fig.show()

Keep in mind that the above function only works for color scales which are defined with an rgba string.

px.colors.cyclical.IceFire would not work because the color is defined with a hex string.