【问题标题】:SeaBorn - choosing hue based on columnSeaBorn - 根据列选择色调
【发布时间】:2021-09-07 22:50:03
【问题描述】:

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

这是我正在尝试做的一个示例:

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>

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

【问题讨论】:

  • 您也可以melt 将数据框从“宽格式”转换为“长格式”:sns.histplot(df.melt(), hue='variable', x='value', multiple='dodge')。请注意,seaborn 0.11.1 multiple= 不适用于宽格式数据帧。

标签: python seaborn data-science visualization


【解决方案1】:

除非我误解了你的要求。

来自docs

如果xy 均未赋值,则将数据集视为宽格式,并为每个数值列绘制直方图

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2023-04-05
    • 2020-06-23
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    相关资源
    最近更新 更多