简体   繁体   English

Plotly 快速折线图 - 获取默认 colors(如何按照字典 object 的指定对线条进行着色?)

[英]Plotly express line chart - get default colors (how to color lines as specified by a dictionary object?)

I have a data frame of a multivariate time series, for which I've created a interactive plotly express plot.我有一个多元时间序列的数据框,为此我创建了一个交互式 plotly express plot。 I'm adding vertical lines at particular locations specified by a dictionary, each line associated to one of the time series, and wish to set the line color to agree with that of the corresponding variable.我在字典指定的特定位置添加垂直线,每条线都与时间序列之一相关联,并希望将线颜色设置为与相应变量的颜色一致。 In essence, in the picture below, each vertical segment can be identified with one of Fp1 or Fp2 and I want to color it as red or black accordingly:本质上,在下图中,每个垂直段都可以用 Fp1 或 Fp2 之一来识别,我想相应地将其着色为红色或黑色:

在此处输入图像描述

First I plot the data frame, where X is my time series matrix, plotly.express has been imported as px, pandas imported as pd, and channels=['Fp1','Fp2']:首先我 plot 数据框,其中 X 是我的时间序列矩阵,plotly.express 已导入为 px,pandas 导入为 pd,并且 channels']:'Fp1'

df=pd.DataFrame(X,columns=channels)
df['id'] = df.index
df = pd.melt(df, id_vars='id', value_vars=df.columns[:-1])
fig = px.line(df, x='id', y='value', color='variable',labels = {'id':'time (20K~100 sec)', 
'value':'millivolts','variable':'channel'},title='Patient')

Subsequent calculations yield a dictionary, HFOs, where each key corresponds is one of my two channels, and each value is a list of times, eg something of the form随后的计算产生一个字典,HFO,其中每个键对应的是我的两个通道之一,每个值是时间列表,例如某种形式

HFOs={'Fp1':[500,....,10500],'Fp2':[800,...11000]}

I then created my lines and added them to the figure:然后我创建了我的线条并将它们添加到图中:

for channel,times in HFOs.items():
    for t in times:        
        fig.add_shape(type='line',yref="y",xref="x",
        x0=t,y0=df['value'].min()*1.2,x1=t,y1=df['value'].max()*1.2,line=dict(color='black', 
        width=.25))
        fig.add_trace(go.Scatter(x=[t],y= 
        [df['value'].max()*1.5],mode='text',showlegend=False))    
fig.show()

This creates the image shown above.这将创建如上所示的图像。 How do I modify line=dict(color='black',width=.25) to change the color to what I want?如何修改 line=dict(color='black',width=.25) 以将颜色更改为我想要的颜色? I wish for the vertical lines at times [500,....,10500]to be blue and times [800,...11000] to be red.我希望 [500,....,10500] 时的垂直线为蓝色,而 [800,...11000] 时的垂直线为红色。 (Of course, in the future, there will be many more channels.) (当然,未来还会有更多的渠道。)

I tried replacing 'black' with 'variable" but that, not surprisingly, just resulted in an error message. I feel there must be a very simple way to achieve my goal.我尝试用“变量”替换“黑色”,但这并不奇怪,只是导致了一条错误消息。我觉得必须有一个非常简单的方法来实现我的目标。

Cool question.很酷的问题。 There might be a better solution, but here's the one I found.可能有更好的解决方案,但这是我找到的解决方案。 Replace the code that creates the vertical lines with the following:将创建垂直线的代码替换为以下代码:

# fetch the colors of the traces from the figure. 
colors = [trace.line["color"] for trace in fig.data]

for inx, (channel,times) in enumerate(HFOs.items()):
    for t in times:        
        fig.add_shape(type='line',yref="y",xref="x",
        x0=t,y0=df['value'].min()*1.2,x1=t,y1=df['value'].max()*2,
                      line=dict(color=colors[inx], width=3))
fig.show()

The resulting figure looks as follows (Random data, made the vertical lines wider for visibility):结果图如下(随机数据,使垂直线更宽以提高可见度):

在此处输入图像描述

An alternative way to get the list of default colors is to use获取默认 colors 列表的另一种方法是使用

px.colors.qualitative.Plotly , which produces a list of 10 hex color codes. px.colors.qualitative.Plotly ,生成 10 个十六进制颜色代码的列表。 My understanding is that these colors would be used for the first 10 series, and then used again for traces 11-20, etc.我的理解是这些 colors 将用于前 10 系列,然后再次用于迹线 11-20 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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