简体   繁体   English

在seaborn中绘制两列dataFrame

[英]Plotting two columns of dataFrame in 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.我正在尝试在 seaborn 中创建一个条形图,它显示数据框中每一行(因子)的两个变量(权重、方差)的值。 Here is what my data looks like:这是我的数据的样子:

    Factor    Weight  Variance
    Growth    10%      0.15
    Value     20%      0.35

Here is my code:这是我的代码:

    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?任何人都有建议或潜在的解决方法?

Thanks谢谢

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM