在Seaborn中绘制两列dataFrame [英] Plotting two columns of dataFrame in seaborn

查看:497
本文介绍了在Seaborn中绘制两列dataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a bar chart in seaborn that displays values for two variables(Weight, Variance) for each row (Factor) in my data frame. Here is what my data looks like:

    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)

以上每次我无法调试时都会抛出错误.我要实现的目标是绘制一个图,每个因子显示两个彩色条.一种颜色的重量(例如:红色)和另一种颜色的差异(例如:蓝色).

The above throws back an error every time that I can't debug. What I am trying to achieve is have one plot, that shows two colored bars for each Factor; weight in one color (ex: red) and variance in another color (ex: blue).

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

Anyone have suggestions or potential workarounds?

谢谢

推荐答案

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

Aside from cleaning up your data into a tidy format, you need to reformat the text data (percentages) into numeric data types. Since that has nothing to do with barplots, I'll assume you can take care of that on your own and focus on the plotting and data structures instead:

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)

这篇关于在Seaborn中绘制两列dataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆