Plotly:如何在情节时间线图中使用两组颜色?

Posted

技术标签:

【中文标题】Plotly:如何在情节时间线图中使用两组颜色?【英文标题】:Plotly: how to use two sets of colors in plotly timeline figure? 【发布时间】:2022-01-08 21:45:44 【问题描述】:

我使用 plotly express 在“y”轴上绘制了一个带有两个标签的时间序列条形图。 “颜色”标签基于第三类。这是简化代码和输出的快照:

import pandas as pd
import datetime
import plotly.express as px

df = pd.DataFrame(dict(
    'beginTime': [
        datetime.datetime.strptime('1/1/2008 1:00:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:15:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 2:00:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:00:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:02:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:20:15', '%m/%d/%Y %H:%M:%S'),
    ],
    'endTime': [
        datetime.datetime.strptime('1/1/2008 1:10:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:35:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 2:07:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:8:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:12:15', '%m/%d/%Y %H:%M:%S'),
        datetime.datetime.strptime('1/1/2008 1:59:15', '%m/%d/%Y %H:%M:%S'),
    ],
    'type': ['1', '1', '1', '2', '2', '2'],
    'activity': ['competition1', 'competition1', 'competition1', 'competition2', 'competition2', 'competition2'],
    'label': ['eat', 'sleep', 'write', 'write', 'code', 'sleep']

))
fig = px.timeline(df, x_start="beginTime", x_end="endTime", y="type", color='label')
fig.show()

我的问题是,

    如何为“y”轴上的每个标签应用两组单独的颜色(这样颜色不会重叠)? 或者如何将两组“颜色”图应用到一个时间线图中?

【问题讨论】:

能否提供minimal reproducible example。 已添加。还改写了最后一个问题。 【参考方案1】: 您可以为每个 y 创建轨迹,为每个 y 使用不同的颜色序列 使用图形对象整合这些轨迹
import pandas as pd
import datetime
import plotly.express as px
import inspect
import plotly.graph_objects as go

df = pd.DataFrame(
    dict(
        
            "beginTime": [
                datetime.datetime.strptime("1/1/2008 1:00:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:15:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 2:00:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:00:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:02:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:20:15", "%m/%d/%Y %H:%M:%S"),
            ],
            "endTime": [
                datetime.datetime.strptime("1/1/2008 1:10:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:35:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 2:07:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:8:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:12:15", "%m/%d/%Y %H:%M:%S"),
                datetime.datetime.strptime("1/1/2008 1:59:15", "%m/%d/%Y %H:%M:%S"),
            ],
            "type": ["1", "1", "1", "2", "2", "2"],
            "activity": [
                "competition1",
                "competition1",
                "competition1",
                "competition2",
                "competition2",
                "competition2",
            ],
            "label": ["eat", "sleep", "write", "write", "code", "sleep"],
        
    )
)

# create a figure for each y with different color sequence
figs = [
    px.timeline(
        g[1],
        x_start="beginTime",
        x_end="endTime",
        y="type",
        color="label",
        color_discrete_sequence=a:b for a,b in inspect.getmembers(px.colors.qualitative)[cs],
    )
    for g,cs in zip(df.groupby("type"), ["Alphabet","G10"])
]

# integrate it
go.Figure(data=[d for f in figs for d in f.data ], layout=figs[0].layout)

【讨论】:

以上是关于Plotly:如何在情节时间线图中使用两组颜色?的主要内容,如果未能解决你的问题,请参考以下文章

Plotly:如何为每个变量创建具有不同样式和颜色的线图?

在plotly python中为折线图的不同部分添加文本

如何使用 plotly express 向折线图添加点或标记?

Plotly:如何更改情节表达图例的背景颜色?

Plotly Express:处理子图

如何在稀疏点之间插入数据以在 R & plotly 中绘制等高线图