1
votes

I would like to plot a bar chart of categorical data, grouped by series.

E.g. i have data with say 6 columns, here below filled with arbitrary values:

df = pd.DataFrame(np.arange(12).reshape(2,6),columns=['A','B','C','D','E','F'])

   A  B  C  D   E   F
0  0  1  2  3   4   5
1  6  7  8  9  10  11

and I would like to simply plot this simple information as a bar chart where for each of the column names A-F, there will show one bar (with its name on the axis or inline) for row 0 and one bar for row 1, each bar having it's height being the number in the body of the matrix for that row and column.

Like normal grouped bar charts, all bars for row 0 should be a different color than those for row 1. Simply put, just a histogram for the categories A-F grouped by the two rows.

Actually in my real data each row has a real name instead of just an index number which is here 0 and 1 for the two rows.

I've found no way so far to get this done with seaborn, and only saw some terrible hacks with matplotlib itself. What is the simplest way to accomplish this?

Is there a way?

2

2 Answers

2
votes

You may plot the transposed dataframe like df.T.plot.bar().

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.arange(12).reshape(2,6),columns=list("ABCDEF"))

df.T.plot.bar()

plt.show()

enter image description here

Using seaborn this requires to reshape ("melt") the dataframe:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame(np.arange(12).reshape(2,6),columns=list("ABCDEF"))

df2 = pd.melt(df.reset_index(), id_vars=["index"], value_vars=df.columns)

sns.barplot(data=df2, x="variable", y="value", hue="index")

plt.show()

enter image description here

1
votes

You can use plotly to draw grouped bar charts. plotly draw graphs and chart very interactive and attractive. Here is an example.

Import libraries:

import pandas as pd
import numpy as np

import plotly.offline as py
py.init_notebook_mode(connected=True)
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

import plotly.graph_objs as go
import plotly.offline as offline
offline.init_notebook_mode()
import plotly.tools as tls

Create DataFrame :

df = pd.DataFrame(np.arange(12).reshape(2,6),columns=['A','B','C','D','E','F'])

Plot Bar Chart:

trace1 = go.Bar(x=df.columns.tolist(),y=df.loc[0].values,
                name='first_row')

trace2 = go.Bar(x= df.columns.tolist(),y=df.loc[1].values,
                name='second-row')

data = [trace1, trace2]
layout = go.Layout(barmode='group')

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')

enter image description here