You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Plotly实现Z分数与Normal(0,1)分布叠加可视化的更多方案问询

基于Plotly实现Z分数与Normal(0,1)分布叠加可视化的更多方案问询

我手头有来自不同组别(比如教室、建筑这类分组)的Z分数数据集,这次就拿两组来举例,实际场景里组数可能更多。

我一直觉得Z分数不太好直观解读,所以想把这些分数和Normal(0,1)分布曲线叠加在同一张图里,这样就能更清楚地看到这些数据点在正态分布钟形曲线里的位置。我自己用Plotly的go.Bar、双Y轴和go.Line实现了这个效果,具体代码如下:

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots    # to enable multiple y-axes

# For Normal distribution
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

d = {'Name':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 
      'Score':[1, 1.5, 4, 1.5, 1.5, 4, 2.5, 3, 2.5, 4],
      'Building':['b', 'a', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'a'],
      'count':[1,1,1,1,1,1,1,1,1,1]}

df = pd.DataFrame(data=d)
color_map = {'a': 'red', 'b': 'blue'}
df['building_color'] = df['Building'].map(color_map)

# Create figure with secondary y-axis
fig2 = make_subplots(specs=[[{"secondary_y": True}]])

# Add building data and stack the bars
fig2.add_trace(go.Bar(x=df[df['Building']=='a']['Score'], y=df[df['Building']=='a']['count'], name='A'), secondary_y=False)
fig2.add_trace(go.Bar(x=df[df['Building']=='b']['Score'], y=df[df['Building']=='b']['count'], name='B'), secondary_y=False)

fig2.update_layout(barmode='stack', yaxis_range=[0,18])

# Add Normal(0,1)
mean=0
std=1

x=np.linspace(mean-4*std, mean+4*std, 300)
y=norm(loc=mean, scale=std).pdf(x)

normal_data = {'x':x, 'y':y}
normal_df = pd.DataFrame(data=normal_data)

fig2.add_trace(go.Line(x=normal_df['x'], y=normal_df['y']), secondary_y=True)

fig2.update_layout(title='Visualizing z-scores on Normal curve', xaxis_title='Z-score', yaxis_title='Count', yaxis2_title='Prob')

我相信肯定还有其他方法能实现同样的可视化效果,非常想学习大家的不同思路和实现方式。你们会怎么绘制这类图表呢?

Stacked red and blue datapoints with green Normal(0,1) in same plot

备注:内容来源于stack exchange,提问作者eschares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:39:39