繁体   English   中英

Map subplot go.pie plotly marker colors using based pandas column value

[英]Map subplot go.pie plotly marker colors using based pandas column value

我想根据 dataframe 列的值更改标记 colors ,所以我尝试了以下代码:

apc_data_by_ctrlmode = apc_data_df.groupby('CONTROLLERMODE')
apc_data_ctrlmode_ON = apc_data_by_ctrlmode.get_group('ON')

map_mv_status_colors ={'SERVICE':'green',
                       'ON':'yellow',
                       'WOUND UP HI':'orange',
                       'WOUND UP LO':'pink',
                       'FFWD':'red',
                       'INIT':'blue'}

map_mv_status_colors_to_series = pd.Series(map_mv_status_colors)

# Create subplots: use 'domain' type for Pie subplot
fig2 = make_subplots(rows=2, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}],
                                            [{'type':'domain'},{'type':'domain'}]])
fig2.add_trace(go.Pie(labels=apc_data_ctrlmode_ON['MV1.STATUS'], name="MV1 Status"),1, 1)

fig2.add_trace(go.Pie(labels=apc_data_ctrlmode_ON['MV2.STATUS'], name="MV2 Status"),1, 2)

fig2.add_trace(go.Pie(labels=apc_data_ctrlmode_ON['MV3.STATUS'], name="MV3 Status"),2, 1)

fig2.add_trace(go.Pie(labels=apc_data_ctrlmode_ON['MV4.STATUS'], name="MV4 Status"),2, 2)

# Use `hole` to create a donut-like pie chart
fig2.update_traces(hole=.5, hoverinfo="label+percent+name",
                   marker_colors=map_mv_status_colors_to_series)

fig2.update_layout(
    title={
        'text': "MV STATUS: VAZÃO DE POLPA E LTH",
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='MV1', x=0.19, y=0.83, font_size=20, showarrow=False),
                 dict(text='MV2', x=0.805, y=0.83, font_size=20, showarrow=False),
                 dict(text='MV3', x=0.19, y=0.18, font_size=20, showarrow=False),
                 dict(text='MV4', x=0.805, y=0.18, font_size=20, showarrow=False)])

fig2.show()

但是饼图没有显示定义的 colors 标记,但图片中的那个:

阴谋

你很接近,在每个跟踪中使用标签然后定义 colors

# Use `hole` to create a donut-like pie chart
# fig2.update_traces(
#     hole=0.5,
#     hoverinfo="label+percent+name",
#     marker_colors=map_mv_status_colors_to_series,
# )
fig2.for_each_trace(
    lambda t: t.update(
        hole=0.5,
        hoverinfo="label+percent+name",
        marker_colors=pd.Series(t.labels).map(map_mv_status_colors),
    ).values
)

完整代码

  • 包括模拟 dataframe
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots

map_mv_status_colors = {
    "SERVICE": "green",
    "ON": "yellow",
    "WOUND UP HI": "orange",
    "WOUND UP LO": "pink",
    "FFWD": "red",
    "INIT": "blue",
}

# simulate dataframe
apc_data_df = pd.DataFrame(
    {
        **{"CONTROLLERMODE": np.random.choice(["ON", "OFF"], 100)},
        **{
            f"MV{i}.STATUS": np.random.choice(list(map_mv_status_colors.keys()), 100)
            for i in range(1, 5)
        },
    }
)

apc_data_by_ctrlmode = apc_data_df.groupby("CONTROLLERMODE")
apc_data_ctrlmode_ON = apc_data_by_ctrlmode.get_group("ON")

# Create subplots: use 'domain' type for Pie subplot
fig2 = make_subplots(
    rows=2,
    cols=2,
    specs=[
        [{"type": "domain"}, {"type": "domain"}],
        [{"type": "domain"}, {"type": "domain"}],
    ],
)
fig2.add_trace(
    go.Pie(labels=apc_data_ctrlmode_ON["MV1.STATUS"], name="MV1 Status"), 1, 1
)

fig2.add_trace(
    go.Pie(labels=apc_data_ctrlmode_ON["MV2.STATUS"], name="MV2 Status"), 1, 2
)

fig2.add_trace(
    go.Pie(labels=apc_data_ctrlmode_ON["MV3.STATUS"], name="MV3 Status"), 2, 1
)

fig2.add_trace(
    go.Pie(labels=apc_data_ctrlmode_ON["MV4.STATUS"], name="MV4 Status"), 2, 2
)

# Use `hole` to create a donut-like pie chart
# fig2.update_traces(
#     hole=0.5,
#     hoverinfo="label+percent+name",
#     marker_colors=map_mv_status_colors_to_series,
# )
fig2.for_each_trace(
    lambda t: t.update(
        hole=0.5,
        hoverinfo="label+percent+name",
        marker_colors=pd.Series(t.labels).map(map_mv_status_colors),
    ).values
)

fig2.update_layout(
    title={
        "text": "MV STATUS: VAZÃO DE POLPA E LTH",
        "y": 0.9,
        "x": 0.5,
        "xanchor": "center",
        "yanchor": "top",
    },
    # Add annotations in the center of the donut pies.
    annotations=[
        dict(text="MV1", x=0.19, y=0.83, font_size=20, showarrow=False),
        dict(text="MV2", x=0.805, y=0.83, font_size=20, showarrow=False),
        dict(text="MV3", x=0.19, y=0.18, font_size=20, showarrow=False),
        dict(text="MV4", x=0.805, y=0.18, font_size=20, showarrow=False),
    ],
)

fig2

在此处输入图像描述

暂无
暂无

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

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