Search code examples
pythonplotlytransparency

Increase transparency/contrast for selection tool in plotly


I am producing some scatter matrix plots with Plotly. Consider this MWE

import pandas
import numpy as np
import plotly.express as px

x = np.random.rand(5555)
df = pandas.DataFrame(
    {
        'x': x,
        'a': x + np.random.rand(len(x)),
        'b': x**3 + np.random.rand(len(x)),
    }
)

px.scatter_matrix(
    df,
).show()

which produces this plot:

enter image description here

Now if I use the lazo selection tool within the plot, the selected points are highlighted while the non selected points gain some transparency:

enter image description here

The thing is that there are so many points that it is not easy to see the difference. Is it possible to increase the transparency of the non selected points? Eventually I would be happy to set the transparency to 100% so they completely disappear.


Solution

  • I found some helpful information here: Extracting plotly.express selection in JupyterLab

    Basically all you have to do is to make your figure object available and update your graph object (which is go.Splom in the case of a scatter_matrix):

    scatter = px.scatter_matrix(df)
    scatter.data[0].update(selected=dict(marker=dict(color='red')),
                           unselected=dict(marker=dict(color='blue',
                                                       opacity=0.001)))
    

    I didnt manage to make the unselected color entirely transparent by setting the opacity to 0, but i am sure you find a way, for example by setting the color to the background color or something.


    Edit: maybe a sloppy solution, but you can simply set the opacity to 0.001 to make the unselected points disappear entirely