Search code examples
pythonplotly

How to dash all but one line in plotly distplot?


I want to use plotly's distribution functions to emphasize the behavior of one variable relative to the others, so I want only this one to be a solid line while the rest are dashed. For instance, in this plotly example:

import plotly.figure_factory as ff
import numpy as np

x1 = np.random.randn(200) - 1
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 1

hist_data = [x1, x2, x3]

group_labels = ['Group 1', 'Group 2', 'Group 3']
colors = ['#333F44', '#37AA9C', '#94F3E4']

# Create distplot with curve_type set to 'normal'
fig = ff.create_distplot(hist_data, group_labels, show_hist=False, colors=colors, show_rug =False)
fig.show()

Which results in enter image description here

How do I make the lines of Group 2 and Group 3 dashed?


Solution

  • Update the traces and use selector for each one:

    import plotly.figure_factory as ff
    import numpy as np
    
    x1 = np.random.randn(200) - 1
    x2 = np.random.randn(200)
    x3 = np.random.randn(200) + 1
    
    hist_data = [x1, x2, x3]
    
    group_labels = ['Group 1', 'Group 2', 'Group 3']
    colors = ['#3344FF', '#FFFFFF', '#AF0000']
    
    # Create distplot with curve_type set to 'normal'
    fig = ff.create_distplot(hist_data, group_labels, show_hist=False, colors=colors, show_rug =False)
    
    fig.update_traces(selector={"name": "Group 1"},
                      line={'dash': 'dash'}).update_traces(
                      selector={"name": "Group 3"},
                      line={'dash': 'dash'})
    
    fig.show()
    

    enter image description here