Pandas Plotting with Multi-Index Pandas Plotting with Multi-Index python python

Pandas Plotting with Multi-Index


I found the unstack(level) method to work perfectly, which has the added benefit of not needing a priori knowledge about how many Codes there are.

ax = dfg.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(9, 7), layout=(2, 3))plt.tight_layout()

enter image description here


Using the following DataFrame ...

DataFrame

# using pandas version 0.14.1from pandas import DataFrameimport pandas as pdimport matplotlib.pyplot as pltdata = {'ColB': {('A', 4): 3.0,('C', 2): 0.0,('B', 4): 51.0,('B', 1): 0.0,('C', 3): 0.0,('B', 2): 7.0,('Code', 'Month'): '',('A', 3): 5.0,('C', 1): 0.0,('C', 4): 0.0,('B', 3): 12.0},'ColA': {('A', 4): 66.0,('C', 2): 5.0,('B', 4): 125.0,('B', 1): 5.0,('C', 3): 41.0,('B', 2): 52.0,('Code', 'Month'): '',('A', 3): 22.0,('C', 1): 14.0,('C', 4): 51.0,('B', 3): 122.0}}df = DataFrame(data)

... you can plot the following (using cross-section):

f, a = plt.subplots(3,1)df.xs('A').plot(kind='bar',ax=a[0])df.xs('B').plot(kind='bar',ax=a[1])df.xs('C').plot(kind='bar',ax=a[2])

enter image description here

One for A, one for B and one for C, x-axis: 'Month', the bars are ColA and ColB.Maybe this is what you are looking for.


  • Creating the desired visualization is all about shaping the dataframe to fit the plotting API.
    • seaborn can easily aggregate long form data from a dataframe without .groupby or .pivot_table.
  • Given the original dataframe df, the easiest option is the convert it to a long form with pandas.DataFrame.melt, and then plot with seaborn.catplot, which is a high-level API for matplotlib.
    • Change the default estimator from mean to sum
  • The 'Month' column in the OP is a string type. In general, it's better to convert the column to datetime dtype with pd._to_datetime
  • Tested in python 3.8.11, pandas 1.3.2, matplotlib 3.4.2, seaborn 0.11.2

seaborn.catplot

import seaborn as snsdfm = df.melt(id_vars=['Month', 'Code'], var_name='Cols')     Month Code  Cols  value0  2014-03    C  ColA     591  2014-01    A  ColA     242  2014-02    C  ColA     773  2014-04    B  ColA    1144  2014-01    C  ColA     67# specify row and col to get a plot like that produced by the accepted answersns.catplot(kind='bar', data=dfm, col='Code', x='Month', y='value', row='Cols', order=sorted(dfm.Month.unique()),            col_order=sorted(df.Code.unique()), estimator=sum, ci=None, height=3.5)

enter image description here

sns.catplot(kind='bar', data=dfm, col='Code', x='Month', y='value', hue='Cols', estimator=sum, ci=None,            order=sorted(dfm.Month.unique()), col_order=sorted(df.Code.unique()))

enter image description here

pandas.DataFrame.plot

  • pandas uses matplotlib and the default plotting backend.
  • To produce the plot like the accepted answer, it's better to use pandas.DataFrame.pivot_table instead of .groupby, because the resulting dataframe is in the correct shape, without the need to unstack.
dfp = df.pivot_table(index='Month', columns='Code', values=['ColA', 'ColB'], aggfunc='sum')dfp.plot(kind='bar', subplots=True, rot=0, figsize=(9, 7), layout=(2, 3))plt.tight_layout()

enter image description here