简体   繁体   中英

How to make duplicated lines visible in Plotly

I went through Plotly Pythons documentation and could find a way to do it. I am trying to plot over 1000 lines and some of it plots on top of each other. I want to see duplicated lines. I tried passing random line width, but sometimes most bold line plots on top. Tried making lines transparent did not work as well. Please advise I inserted simple example below:

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

在此处输入图片说明

You can iterate over the lines in descending order of their thickness. You can start with a max_width and reduce from there for every new line being plotted. I created a sample script for 10 lines with a linear color scheme.

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

Here, we plot the same line over and over again but with varying thicknesses and varying colors. The output is as shown below: 在此处输入图片说明

This feels like a very open question with regards to why you would want to plot duplicated series, how you end up with duplicates at all. But we'll leave that for now. If it's the case that you can end up with 1000 duplicates, I would use different line widths for each series, and a very low opacity a in 'rgba(0, 0, 255, {a}' . You could also use a varying opacity for each line, but you don't have to. Here's one way of doing it if you've got duplicated values in df_dupe and some unique series in df . Dupes ar displayed in shades of blue . I'd be happy to go into other details if this is something you can use.

Plot:

在此处输入图片说明

Complete code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM