我在我的图中添加了多个
go.Box()
,我希望x轴标签与图例不同。我该怎么做?
更准确地说:
细节和背景
我有以下熊猫数据框:
method group patient device_field_strength variable value
0 expert1 experts 62 device_field_strength_1.5 F1_score 0.857
1 expert1 experts 66 device_field_strength_3 F1_score 0.909
... ... ... ... ... ... ...
和以下代码:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = go.Figure()
fig = make_subplots(rows=1, cols=2, subplot_titles=("4 experts", "4 methods with highest F1 score"))
device_field_strengths = device_field_strength_impact_df.device_field_strength.unique()
for group in ['experts', 'best_methods']:
for metric in ['F1_score', 'Dice']:
for device_field_strength in device_field_strengths:
fig.add_trace(go.Box(
y=device_field_strength_impact_df[(device_field_strength_impact_df.device_field_strength==device_field_strength) & (device_field_strength_impact_df.group==group) & (device_field_strength_impact_df.variable==metric)].value,
name=f'{device_field_strength} {metric}',
text=group,
legendgroup=metric,
marker_color='darkblue' if metric == 'F1_score' else 'red',
boxmean=True # represent mean
), row=1, col=1 if group == 'experts' else 2)
fig.show()
注意:我用 Plotly express 制作了 相同的图形,但我认为它不能显示平均值。
更新:r-beginners 向我解释了如何使用 plotly express 添加均值:只需在
fig.update_traces(boxmean=True)
之后添加 px.box()
!谢谢 r-初学者 :)