python plotly violin plot显示负尾

python plotly violin plot shows negative tail

python plotly violin plot 显示负尾,但没有低于 0 的值。实际 y 轴在 0 到 23 之间(24 小时间隔)。但是小提琴图的尾部大于 23 且小于 0(图表中添加了第 0 和 23 个边距水平)。

代码:

fig = go.Figure()

fig.add_trace(go.Violin(x=df['Region'][ df['alarm_severity'] == 'CRITICAL' ],
                        y=df['OccurredTime'][ df['alarm_severity'] == 'CRITICAL' ],
                        legendgroup='CRITICAL', scalegroup='CRITICAL', name='CRITICAL',
                        line_color='blue')
             )

fig.add_trace(go.Violin(x=df['Region'][ df['alarm_severity'] == 'MAJOR' ],
                        y=df['OccurredTime'][ df['alarm_severity'] == 'MAJOR' ],
                        legendgroup='MAJOR', scalegroup='MAJOR', name='MAJOR',
                        line_color='red')
             )
fig.add_trace(go.Violin(x=df['Region'][ df['alarm_severity'] == 'WARNING' ],
                        y=df['OccurredTime'][ df['alarm_severity'] == 'WARNING' ],
                        legendgroup='WARNING', scalegroup='WARNING', name='WARNING',
                        line_color='green')
             )


fig.add_trace(go.Violin(x=df['Region'][ df['alarm_severity'] == 'MINOR' ],
                        y=df['OccurredTime'][ df['alarm_severity'] == 'MINOR' ],
                        legendgroup='MINOR', scalegroup='MINOR', name='MINOR',
                        line_color='orange')
             )



fig.update_traces(box_visible=True, meanline_visible=True )
fig.update_layout(violinmode='group',width=1000,
    height=600)



fig.show()

如果您查看 Violin 文档 (https://plotly.com/python-api-reference/generated/plotly.graph_objects.Violin.html),您可能需要向所有 Violin 初始化添加 spanmode = 'hard'。这样,您的最大和最小跨度由您的数据设置,而不是 Silverman 的经验法则(带宽)。

spanmode设置计算密度函数的数据跨度 space 的方法。 “软”表示跨度从样本的最小值减去两个带宽到样本的最大值加上两个带宽。 “硬”意味着跨度从样本的最小值到最大值。对于自定义跨度设置,使用模式“手动”并填写跨度属性。