我如何使用Plotly Treemap覆盖单个盒子颜色

发布于 2025-02-03 07:28:50 字数 404 浏览 3 评论 0原文

我正在创建一个带有Plitly的Treemap,该treemap按回购中的大小显示文件,并为目录嵌套。我想根据自己的任意规则分配颜色。例如,“以测试_开头的文件是红色的”或“二进制文件为蓝色”。我不使用dataframe,所以我调用treemap()这样:

fig = px.treemap(names=names, ids=ids, parents=parents, values=values, ...)

我希望通过一个明确具有每个节点的颜色的匹配数组。 treemap() params coloriNCETE_COLOR_MAP似乎无法正常工作?有没有办法指定每个盒子的颜色?

I'm creating a treemap with Plotly that shows files in a repo by their size, with nesting for the directories. I'd like to assign colors based on my own arbitrary rules. For example "files that start with test_ are red" or "binary files are blue". I'm not using a dataframe so I call treemap() like this:

fig = px.treemap(names=names, ids=ids, parents=parents, values=values, ...)

I was hoping to pass in a matching array that explicitly had the color of every node. The treemap() params color and discrete_color_map don't seem to work that way? Is there a way to specify the color of every box?

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

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

发布评论

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

评论(1

享受孤独 2025-02-10 07:28:50

您可以使用IDScolor进行此操作,然后具有color_discrete_map覆盖每个盒子的颜色。此示例基于我的用例有点奇怪,但它有效:

import plotly.express as px

def create_treemap():
    names = ['a', 'b', 'c', 'd']
    ids = ['a', 'a/b', 'a/c', 'a/c/d']
    parents = ['', 'a', 'a', 'a/c']
    values = [10, 5, 5, 2]

    colors = {
        'a': 'red',
        'a/b': 'green',
        'a/c': 'gold',
        'a/c/d': 'grey'
    }

    fig = px.treemap(names=names, ids=ids, parents=parents, values=values,
                    color=ids,
                    color_discrete_map=colors, 
                    branchvalues='total', title="my treemap")
    fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    return fig

create_treemap().show()

它产生:

”结果treemap“

You can do this with ids and color and then have color_discrete_map override the color of every box. This example is a little weird based on my use case, but it works:

import plotly.express as px

def create_treemap():
    names = ['a', 'b', 'c', 'd']
    ids = ['a', 'a/b', 'a/c', 'a/c/d']
    parents = ['', 'a', 'a', 'a/c']
    values = [10, 5, 5, 2]

    colors = {
        'a': 'red',
        'a/b': 'green',
        'a/c': 'gold',
        'a/c/d': 'grey'
    }

    fig = px.treemap(names=names, ids=ids, parents=parents, values=values,
                    color=ids,
                    color_discrete_map=colors, 
                    branchvalues='total', title="my treemap")
    fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    return fig

create_treemap().show()

which produces:

resulting treemap

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