【问题标题】:Create plotly distplot charts in plotly express在 plotly express 中创建 plotly distplot 图表
【发布时间】:2022-01-16 13:28:22
【问题描述】:

我正在尝试创建一个类似于 plotly 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()

【问题讨论】:

    标签: python plotly histogram


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

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

      输出:

      线条令人愉悦地虚线。

      【讨论】:

        猜你喜欢
        • 2021-09-19
        • 2021-07-21
        • 1970-01-01
        • 2021-10-21
        • 2021-12-06
        • 2023-03-19
        • 2022-01-10
        • 2021-10-25
        • 2019-11-04
        相关资源
        最近更新 更多