在 plotly express 中创建 plotly distplot 图表

Create plotly distplot charts in plotly express

我正在尝试创建一个像绘图一样的图形 create_distplot,但是我想将线条样式更改为虚线。查看该函数,我看到他们说该函数已被弃用,我应该改用 plotly.express 函数。

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

但是,我在 plotly.express 示例中找不到任何内容来说明如何创建类似 distplot 的内容。 plotly 网站上的所有示例仍然使用 create_distplot。我可以使用已弃用的功能,除了我想将线条样式调整为虚线,而且我看不到任何更改线条设置的方法,只能更改颜色。具体来说,我正在寻找直方图数据并创建一个直线和地毯图,其中直线显示分布曲线,如下例所示。有人可以帮我弄清楚如何在 plotly.express 中执行此操作或至少如何更改 distplot 的线条样式吗?

这是我正在使用的情节参考。 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()

除了 .update_layout(),您还可以使用 .update_traces():

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

完整代码:

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()

输出:

线条很漂亮。

您可以更新虚线:

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)