4

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. https://plotly.com/python/distplot/#basic-distplot

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 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I got a LinAlgError from one distribution if I try to create all using create_displot (three distributions like in the example). However, if I give only the one that raises the error for create_displot it runs normally. I did not find anything that help me to understand what going on that breaks when computing the density with more than one dist, but it alone works.
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.