Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 979
Vikram Josyula Avatar asked Nov 29 '16 22:11

Vikram Josyula


People also ask

How do you plot multiple columns in Seaborn Barplot?

We can make multiple columns of the barplot by using the seaborn function group bar. The groupby() method in Pandas is used to divide data into groups depending on specified criteria. In the following example script, we have included the matplotlib library and seaborn module for plotting multiple columns using barplot.

How do you plot multiple plots in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


1 Answers

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)

enter image description here

like image 121
Paul H Avatar answered Nov 12 '22 07:11

Paul H