Create plotly distplot charts in plotly express

I'm trying to create a figure like a plotly create_distplot, however I'd like to change the line style to dashed lines. Looking into the function, I see that they say that the function is deprecated and I should use plotly.express functions instead.

**this function is deprecated**, use instead :mod:`plotly.express` functions 

However, I can't find anything in plotly.express examples that shows how to create something like the distplot. All of the examples on the plotly website still use create_distplot. I'm fine with using the deprecated function, except that I'd like to adjust the line style to have a dashed line, and I don't see any way to change the line settings, only the colors. Specifically, I'm looking to take histogram data and create a line and rug plot, where the line shows the distribution curve, like in the example below. Can someone please help me figure out how to do this in plotly.express or at least how to change the line style of the distplot?

Here is the plotly reference I'm using.

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) # Add title fig.update_layout(title_text='Curve and Rug Plot') fig.show() 

enter image description here

2 Answers

You can update lines to be dashed:

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) # Add title fig.update_layout(title_text='Curve and Rug Plot') fig.for_each_trace(lambda t: t.update(line={"dash":"dash"}) if t.mode=="lines" else t) 
1

Alongside .update_layout(), you can use .update_traces():

fig.update_traces(line={'dash': 'dash'}) 

Full code:

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) # Add title fig.update_layout(title_text='Curve and Rug Plot') fig.update_traces(line={'dash': 'dash'}) fig.show() 

Output:

enter image description here

The lines are delightfully dashed.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like