【问题标题】:How to dash all but one line in plotly distplot?如何在 plotly distplot 中划破除一条线以外的所有线?
【发布时间】:2023-01-05 22:58:16
【问题描述】:

我想使用 plotly 的分布函数来强调一个变量相对于其他变量的行为,所以我只希望这条是实线,而其余的是虚线。例如,在这个情节示例中:

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, show_rug =False)
fig.show()

结果是

如何使第 2 组和第 3 组的线变为虚线?

【问题讨论】:

    标签: python plotly


    【解决方案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 = ['#3344FF', '#FFFFFF', '#AF0000']
    
    # Create distplot with curve_type set to 'normal'
    fig = ff.create_distplot(hist_data, group_labels, show_hist=False, colors=colors, show_rug =False)
    
    fig.update_traces(selector={"name": "Group 1"},
                      line={'dash': 'dash'}).update_traces(
                      selector={"name": "Group 3"},
                      line={'dash': 'dash'})
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多