簡體   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