SeaBorn基于列的色调选择

2024-10-01 13:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

具有预测(列)值和实际(列)值的DF。我想绘制一个叠加的直方图,它与使用色调属性时具有相同的外观。如果不重建原始数据,我无法找到这样做的方法

下面是我尝试做的一个例子:

df = pd.DataFrame({'A':np.random.uniform(low=0.0, high=9.0, size=(150,)),'P':np.random.uniform(low=0.0, high=9.0, size=(150,))})
actual = df.A.to_frame()
predicted = df.P.to_frame()

print(df.head())

actual.columns = ['value']
actual['t'] = 'A'

predicted.columns = ['value']
predicted['t'] = 'P'
tmp = pd.concat([actual,predicted])

print(tmp.head())

sns.histplot(data=tmp,x='value' ,hue="t")

Output:
Original DF
          A         P
0  2.546046  2.503833
1  4.797077  2.306720
2  1.358222  4.839675
3  7.063206  8.828486
4  3.010978  7.406337

Manipulated DF
      value  t
0  2.546046  A
1  4.797077  A
2  1.358222  A
3  7.063206  A
4  3.010978  A
<matplotlib.axes._subplots.AxesSubplot at 0x7fd657112fd0>

enter image description here

Q:如何在不操纵原始DF的情况下获得类似的结果


Tags: todfsizevaluenprandomuniformframe
1条回答
网友
1楼 · 发布于 2024-10-01 13:39:09

除非我误解了你的要求

docs开始:

If neither x nor y is assigned, the dataset is treated as wide-form, and a histogram is drawn for each numeric column

df = pd.DataFrame(
    {
        "A": np.random.uniform(low=0.0, high=9.0, size=(150,)),
        "P": np.random.uniform(low=0.0, high=9.0, size=(150,)),
    }
)
ax = sns.histplot(df)

enter image description here

相关问题 更多 >