【问题标题】:How to make duplicated lines visible in Plotly如何使重复的线条在 Plotly 中可见
【发布时间】:2021-09-12 04:58:38
【问题描述】:

我浏览了 Plotly Pythons 文档并找到了一种方法。我正在尝试绘制超过 1000 条线,其中一些线相互重叠。我想看到重复的行。我尝试传递随机线宽,但有时最粗的线图在顶部。尝试使线条透明也不起作用。请告知我在下面插入了简单的示例:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [10, 8, 6, 4, 2, 0, 2, 4, 2, 0]

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=x, y=y,
    line_color='red',
    name='Duplicate1',
))
fig.add_trace(go.Scatter(
    x=x, y=y,
    line_color='rgb(231,107,243)',
    name='Duplicate2',
))

fig.update_traces(mode='lines')
fig.show()

【问题讨论】:

  • 建议的解决方案对您来说效果如何?

标签: python plotly


【解决方案1】:

您可以遍历descending order 中的线条的粗细。您可以从max_width 开始,然后从那里减少每条新线的绘制。我用线性配色方案为 10 行创建了一个示例脚本。

import plotly.graph_objects as go


x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [10, 8, 6, 4, 2, 0, 2, 4, 2, 0]

fig = go.Figure()

max_thickness = 100


N = 10 
for i in range(N):
    
    fig.add_trace(go.Scatter(
    x=x, y=y,
    line_color='rgb({r},255,255)'.format(r= (255//N)*i )  ,
    name='Duplicate ' + str(i),
    line=dict(width=max_thickness - (i*10) ) 
    ))

fig.update_traces(mode='lines')
fig.show()

在这里,我们一遍又一遍地绘制同一条线,但具有不同的粗细和不同的颜色。输出如下图所示:

【讨论】:

    【解决方案2】:

    这感觉像是一个非常开放的问题,关于为什么你想要绘制重复的系列,你最终是如何得到重复的。但我们现在暂且不谈。如果您最终会得到1000 重复项,我会为每个系列使用不同的线宽,并在'rgba(0, 0, 255, {a}' 中使用非常低的不透明度a。您也可以为每一行使用不同的不透明度,但您不必这样做。如果您在df_dupe 中有重复值并且在df 中有一些独特的系列,这是一种方法。 Dupes 以blue 的阴影显示。如果您可以使用此功能,我很乐意提供其他详细信息。

    剧情:

    完整代码:

    from plotly.subplots import make_subplots
    import plotly.graph_objs as go
    import pandas as pd
    import numpy as np
    
    # random data
    np.random.seed(123)
    frame_rows = 100
    frame_cols = 3
    frame_columns = ['V_'+str(e) for e in list(range(frame_cols+1))]
    df=pd.DataFrame()
    
    dupe_rows = 100
    dupe_cols = 1000
    dupe_columns = ['D_'+str(e) for e in list(range(dupe_cols+1))]
    df_dupe=pd.DataFrame()
    
    
    # rng = range(fra)
    
    # dupe data
    for i, col in enumerate(dupe_columns):
        df_dupe[col]=np.sin(np.arange(0,frame_rows/10, 0.1))#*np.random.uniform(low=0.1, high=0.99))
        
    # non-dupe data    
    for i, col in enumerate(frame_columns):
        df[col]=np.sin(np.arange(0,frame_rows/10, 0.1))*((i+1)/5)
    
        fig = go.Figure()
    
    # calculations for opacity, colors and widths for duped lines
    N = len(dupe_columns)
    opac = []
    colors = []
    max_width = 50
    widths = []
    
    # colors and widths
    for i, col in enumerate(dupe_columns):
        a = (1/N)*(i+1)
        opac.append(a)
        colors.append('rgba(0,0,255, '+str(a)+')')
        #widths2 = N/(i+1)
        widths.append(max_width/(i+1)**(1/2))
    
    # line and colors for duplicated values
    fig = go.Figure()
    for i, col in enumerate(dupe_columns):
        fig.add_traces(go.Scatter(x=df_dupe.index, y = df_dupe[col], mode = 'lines',
    #                               line_color = colors[i],
                                  line_color ='rgba(0,0,255, 0.05)',
                                  line_width = widths[i]))
    
    # highlight one of the dupe series
    fig.add_traces(go.Scatter(x=df_dupe.index, y = df_dupe[col], mode = 'lines',
                                  line_color ='rgb(0,0,255)',
                                  line_width = 3))
    
    # compare dupes to some other series
    for i, col in enumerate(frame_columns[-3:]):
        fig.add_traces(go.Scatter(x=df.index, y = df[col], mode = 'lines',
    #                               line_color = colors[i],
    #                               line_width = widths[i]
                                 ))
    
    fig.update_yaxes(range=[-1.3, 1.3])
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2012-04-09
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 2021-08-24
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多