Troy D
Troy D

Reputation: 2245

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. 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

Upvotes: 4

Views: 10622

Answers (2)

Rob Raymond
Rob Raymond

Reputation: 31236

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)

Upvotes: 1

zabop
zabop

Reputation: 7922

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.

Upvotes: 1

Related Questions