pandas 为流程创建热图

myzjeezk  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(142)

我有个问题我想创建一个带有热图的进程。看看每走一步需要多长时间。我用PyDot创建了这个进程,并为每个步骤创建了一个dataframe
如何为我的流程创建热图?
计算还应包括从步骤到步骤的时间。所以你可以计算边的时间,例如task1_start - start/task2_start - task1_end,你可以计算节点的时间,例如。task1_end - task1_start / task2_end - task2_start

import pydot
from IPython.display import SVG

graph = pydot.Dot(graph_type='digraph')

start_node = pydot.Node("Start", shape="circle")
end_node = pydot.Node("End", shape="doublecircle")
task_node1 = pydot.Node("Task1", shape="box")
task_node2 = pydot.Node("Task2", shape="box")

graph.add_node(start_node)
graph.add_node(end_node)
graph.add_node(task_node1)
graph.add_node(task_node2)

start_to_task1_edge = pydot.Edge("Start", "Task1")
task1_to_task2_edge = pydot.Edge("Task1", "Task2")
task2_to_end_edge = pydot.Edge("Task2", "End")

graph.add_edge(start_to_task1_edge)
graph.add_edge(task1_to_task2_edge)
graph.add_edge(task2_to_end_edge)

graph.write_svg("diagram.svg")
SVG('diagram.svg')

import pandas as pd 
d = {'id': [1, 1, 1, 1, 1, 1,
            2, 2, 2, 2, 2, 2],
    'step': ['Start', 'Task_Start1', 'Task_End1', 'Task_Start2', 'Task_End2', 'End',
              'Start', 'Task_Start1', 'Task_End1', 'Task_Start2', 'Task_End2', 'End'],
     'timestamp': ['2023-01-01', '2023-01-05', '2023-01-10', '2023-01-12', '2023-02-12', '2023-02-14',
               '2023-01-01', '2023-01-05', '2023-01-10', '2023-01-12', '2023-01-15', '2023-02-16',]}

df  = pd.DataFrame(data=d,)


    id         step   timestamp
0    1        Start  2023-01-01
1    1  Task_Start1  2023-01-05
2    1    Task_End1  2023-01-10
3    1  Task_Start2  2023-01-12
4    1    Task_End2  2023-02-12
5    1          End  2023-02-14
6    2        Start  2023-01-01
7    2  Task_Start1  2023-01-05
8    2    Task_End1  2023-01-10
9    2  Task_Start2  2023-01-12
10   2    Task_End2  2023-01-15
11   2          End  2023-02-16
ryevplcw

ryevplcw1#

要绘制热图,请使用SVG导出并向节点添加类名以标记它们的热度。然后,您可以将SVG组包含两次,并使用过滤器来给予类似于热图的内容,方法是将背景填充颜色和模糊,并将正常的白色版本作为前景。

<svg height="110" width="110" xmlns="http://www.w3.org/2000/svg">

    <style>
        .foreground *{
        fill: none;
        stroke: black;
        }
        .foreground text{
        fill: black;
        stroke: none;
        }
        .background *{
        stroke: none;
        }

        .background text { display: none; }
        .background .heat-1 { fill: #00ff00; }
        .background *.heat-2 { fill: #ff0000; }
    </style>

    <defs>
        <filter id="f1" x="0" y="0">
            <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
        </filter>

    </defs>
    <g transform="scale(1 1) rotate(0) translate(4 112)">
        <g class='background' filter='url(#f1)'>

            <g id="graph0" class="graph">
                <!-- a -->
                <g id="node1" class="node heat-1">
                    <title>a</title>
                    <ellipse cx="27" cy="-90" rx="27" ry="18" />
                    <text text-anchor="middle" x="27" y="-86.3" font-family="Times,serif"
                        font-size="14.00">a</text>
                </g>
                <!-- b -->
                <g id="node2" class="node heat-2">
                    <title>b</title>
                    <ellipse cx="27" cy="-18" rx="27" ry="18" />
                    <text text-anchor="middle" x="27" y="-14.3" font-family="Times,serif"
                        font-size="14.00">b</text>
                </g>
                <!-- a&#45;&gt;b -->
                <g id="edge1" class="edge">
                    <title>a&#45;&gt;b</title>
                    <path d="M27,-71.7C27,-63.98 27,-54.71 27,-46.11" />
                    <polygon fill="black" stroke="black"
                        points="30.5,-46.1 27,-36.1 23.5,-46.1 30.5,-46.1" />
                </g>
            </g>
        </g>
        <g class='foreground'>

            <g id="graph0" class="graph" >
                <!-- a -->
                <g id="node1" class="node heat-1">
                    <title>a</title>
                    <ellipse cx="27" cy="-90" rx="27" ry="18" />
                    <text text-anchor="middle" x="27" y="-86.3" font-family="Times,serif"
                        font-size="14.00">a</text>
                </g>
                <!-- b -->
                <g id="node2" class="node heat-2">
                    <title>b</title>
                    <ellipse cx="27" cy="-18" rx="27" ry="18" />
                    <text text-anchor="middle" x="27" y="-14.3" font-family="Times,serif"
                        font-size="14.00">b</text>
                </g>
                <!-- a&#45;&gt;b -->
                <g id="edge1" class="edge">
                    <title>a&#45;&gt;b</title>
                    <path d="M27,-71.7C27,-63.98 27,-54.71 27,-46.11" />
                    <polygon fill="black" stroke="black"
                        points="30.5,-46.1 27,-36.1 23.5,-46.1 30.5,-46.1" />
                </g>
            </g>
        </g>
    </g>
</svg>

如果你可以两次使用一个引用到图形组,而不是两次包含它,那就太好了,但是如果使用而不是内联,我无法获得一个CSS表达式来区别对待这个组。

相关问题