user8200327
user8200327

Reputation:

Plot horizontal bar plot with seaborn

I'm crawling a directory and reading a bunch of files and parsing them. All I need is to get the size of the dataframe. I do so by using len(df.index).

Each directory has 10 files, numbered from 0 to 9. I add all these len(df.index) to a dataframe, where the letters['A', 'B', 'C', 'D'] come from a list of categories. These values are added to the dataframe by df2.loc[seed,nd] = len(df.index). The resulting dataframe is as follows:

         A         B         C       D
0  10515.0  160592.0  221621.0  198884.0
1   9777.0  161307.0  222064.0  199841.0
2  10957.0  159954.0  219553.0  198622.0
3  12731.0  157862.0  221250.0       NaN
4  11765.0  162177.0       NaN       NaN
5   8849.0  155631.0       NaN       NaN
6  10549.0  160976.0       NaN       NaN
7   8694.0  158953.0       NaN       NaN
8  11696.0  160952.0       NaN       NaN
9  10590.0  161046.0       NaN       NaN

In my script, I crawl two directories in a for loop, X and Z, resulting in two dataframes like the one above.

The issue is that I'm trying to plot this dataframe with Seaborn horizontal barplot using

sns.barplot(data=df2)

but I don't know how to specify the category, such as shown here.

How can this be accomplished? Do I need to change my dataframe format?

I'd like to result to be like this (from MS Excel)

enter image description here

Upvotes: 7

Views: 37242

Answers (2)

jezrael
jezrael

Reputation: 863481

You can use concat of both DataFrames with parameter keys for specify groups and then reshape by melt, last use parameter hue for specifying groups:

dfs = [df21, df22]

df = pd.concat(dfs, keys=('X','Z')).reset_index(level=0).melt('level_0')
sns.barplot(x='value', y='variable', hue='level_0', data=df)

Upvotes: 1

Divyanshu Srivastava
Divyanshu Srivastava

Reputation: 1507

I think this can be achieved using the orient attribute of seabon's barplot function.

Example -

import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
sns.barplot(data=df, orient = 'h')

example plot

Upvotes: 24

Related Questions