在Plotly Express中创建Plotly Distplot图表

4
我正在尝试创建一个类似于Plotly create_distplot的图形,但是我想将线条样式更改为虚线。查看该函数时,我发现他们说该函数已被弃用,建议我改用plotly.express函数。
**this function is deprecated**, use instead :mod:`plotly.express` functions

然而,我在plotly.express示例中找不到任何类似于distplot的创建方法。所有在plotly网站上的示例仍在使用create_distplot。我可以使用已弃用的函数,但我想调整线条样式为虚线,我没有看到任何更改线条设置的方法,只有颜色。具体来说,我想获取直方图数据并创建线和地毯图,其中线显示分布曲线,就像下面的示例一样。请问有人能帮助我在plotly.express中找出如何执行此操作,或者至少如何更改distplot的线条样式吗?
这是我正在使用的plotly参考链接。 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个回答

1

除了使用.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()

输出:

在此输入图片描述

这些线条非常美观。


1

您可以将线条更新为虚线:

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)

如果我尝试使用create_displot(像示例中的三个分布)创建所有分布,其中一个分布会引发LinAlgError错误。然而,如果我只提供引发错误的那个分布给create_displot,它会正常运行。我没有找到任何可以帮助我理解在计算多个分布的密度时出现问题的原因,但是单独计算它是正常的。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接