在plotly python中定义特定数据的颜色

发布于 2025-01-14 20:51:43 字数 908 浏览 3 评论 0原文

我实现了一个时间轴来显示我的数据。 要创建我使用的时间轴:

fig = px.timeline(df
                  , x_start="Start"
                  , x_end="End"
                  , y="Name"
                  , hover_name="Task"
                  , opacity=0.8
                  , template='plotly_white'
                  , height=len(df)*35
                  , width=1150
                  , color="Type"
                  , category_orders=dict(
                    Name=df["Name"].drop_duplicates().tolist()
                    )
                  )

您可以看到颜色基于“类型”。 在我的数据框中,我有两种类型的数据:发布和史诗。 颜色如下所示:

在此处输入图像描述

这里的问题是颜色是随机生成的。 但是,我想这样定义颜色:

colors = {'Release':'rgb(255,239,0)', 'Epic':'rgb(148, 76, 228)'}
fig.update(color=colors)

有办法做到这一点吗?

I implemented a Timeline to display my data.
To create the Timeline I used:

fig = px.timeline(df
                  , x_start="Start"
                  , x_end="End"
                  , y="Name"
                  , hover_name="Task"
                  , opacity=0.8
                  , template='plotly_white'
                  , height=len(df)*35
                  , width=1150
                  , color="Type"
                  , category_orders=dict(
                    Name=df["Name"].drop_duplicates().tolist()
                    )
                  )

You can see that the colors are based on "Type".
In my dataframe I have two types of Data: Releases and Epics.
The colors look like follows:

enter image description here

The problem here is that colors are generated randomly.
However, I would like to define the colors like this:

colors = {'Release':'rgb(255,239,0)', 'Epic':'rgb(148, 76, 228)'}
fig.update(color=colors)

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2025-01-21 20:51:44
  • 已经模拟了一些数据以适合您的代码
  • 已经注释了高度和宽度,因此它适合我的屏幕
  • 这是使用color_discrete_map
import pandas as pd
import numpy as np
import plotly.express as px

n = 8
df = pd.DataFrame(
    {
        "Start": pd.date_range("1-jan-2021", freq="1M", periods=n),
        "End": pd.date_range("1-mar-2021", freq="1M", periods=n),
        "Name": [str(i) for i in range(n)],
        "Task": np.random.choice(list("ABCDE"), n),
        "Type": np.random.choice(["Epic", "Release"], n),
    }
)

fig = px.timeline(
    df,
    x_start="Start",
    x_end="End",
    y="Name",
    hover_name="Task",
    opacity=0.8,
    template="plotly_white",
    # height=len(df) * 35,
    # width=1150,
    color="Type",
    category_orders=dict(Name=df["Name"].drop_duplicates().tolist()),
    color_discrete_map={"Release": "rgb(255,239,0)", "Epic": "rgb(148, 76, 228)"},
)

fig

在此处输入图像描述

  • have simulated some data to fit your code
  • have commented height and width so it fits on my screen
  • it's a simple case of using color_discrete_map
import pandas as pd
import numpy as np
import plotly.express as px

n = 8
df = pd.DataFrame(
    {
        "Start": pd.date_range("1-jan-2021", freq="1M", periods=n),
        "End": pd.date_range("1-mar-2021", freq="1M", periods=n),
        "Name": [str(i) for i in range(n)],
        "Task": np.random.choice(list("ABCDE"), n),
        "Type": np.random.choice(["Epic", "Release"], n),
    }
)

fig = px.timeline(
    df,
    x_start="Start",
    x_end="End",
    y="Name",
    hover_name="Task",
    opacity=0.8,
    template="plotly_white",
    # height=len(df) * 35,
    # width=1150,
    color="Type",
    category_orders=dict(Name=df["Name"].drop_duplicates().tolist()),
    color_discrete_map={"Release": "rgb(255,239,0)", "Epic": "rgb(148, 76, 228)"},
)

fig

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文