使用基于 pandas 列值的子图 go.pie 绘图标记颜色
Map subplot go.pie plotly marker colors using based pandas column value
我想根据数据框列的值更改标记颜色,所以我尝试了以下代码:
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()
但是饼图没有显示定义的颜色标记,而是附图中的:
你很接近,在每条轨迹中使用标签然后定义颜色
# 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
)
完整代码
- 包括模拟数据帧
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
我想根据数据框列的值更改标记颜色,所以我尝试了以下代码:
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()
但是饼图没有显示定义的颜色标记,而是附图中的:
你很接近,在每条轨迹中使用标签然后定义颜色
# 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
)
完整代码
- 包括模拟数据帧
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