gpt4 book ai didi

python - 在seaborn中绘制两列dataFrame

转载 作者:太空狗 更新时间:2023-10-30 00:31:24 25 4
gpt4 key购买 nike

我正在尝试在 seaborn 中创建一个条形图,它显示数据框中每一行(因子)的两个变量(权重、方差)的值。这是我的数据:

    Factor    Weight  Variance
Growth 10% 0.15
Value 20% 0.35

这是我的代码:

    fig=plt.figure(figsize=(10,10))
ax1=fig.add_subplot(221)
sns.barplot(x=df.index, y=df[['Weight', 'Variance']], ax=ax1)

每次我无法调试时,以上都会返回一个错误。我想要实现的是有一个图,为每个因素显示两个彩色条;一种颜色的权重(例如:红色)和另一种颜色的方差(例如:蓝色)。

有人有建议或潜在的解决方法吗?

谢谢

最佳答案

除了将数据清理成整洁的格式外,您还需要将文本数据(百分比)重新格式化为数字数据类型。由于这与条形图无关,我假设您可以自己处理并专注于绘图和数据结构:

df = pandas.DataFrame({
'Factor': ['Growth', 'Value'],
'Weight': [0.10, 0.20],
'Variance': [0.15, 0.35]
})
fig, ax1 = pyplot.subplots(figsize=(10, 10))
tidy = df.melt(id_vars='Factor').rename(columns=str.title)
seaborn.barplot(x='Factor', y='Value', hue='Variable', data=tidy, ax=ax1)
seaborn.despine(fig)

enter image description here

关于python - 在seaborn中绘制两列dataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com