title: Pandas Plot Grouped Bar Chart by Time
body: <p>I have a table below that I would like to plot in a grouped bar chart. I want the x-axis to be <code>time_period</code> and the y-axis to be <code>death_licenses</code> and I want to be categorized by <code>civic_centre</code>. As you can see, for every distinct <code>time_period</code>, there is four categorical options in <code>civic_centre</code>.</p>
  
  <pre><code>+-------------+--------------+----------------+
  | time_period | civic_centre | death_licenses |
  +-------------+--------------+----------------+
  | 2011-01-01  | ET           |            410 |
  | 2011-01-01  | NY           |            681 |
  | 2011-01-01  | SC           |            674 |
  | 2011-01-01  | TO           |            297 |
  | 2011-02-01  | ET           |            307 |
  | 2011-02-01  | NY           |            388 |
  | 2011-02-01  | SC           |            407 |
  | 2011-02-01  | TO           |            223 |
  | 2011-03-01  | ET           |            349 |
  | 2011-03-01  | NY           |            655 |
  | 2011-03-01  | SC           |            400 |
  | 2011-03-01  | TO           |            185 |
  | 2011-04-01  | ET           |            373 |
  | 2011-04-01  | NY           |            640 |
  | 2011-04-01  | SC           |            457 |
  | 2011-04-01  | TO           |             42 |
  +-------------+--------------+----------------+
  </code></pre>
  
  <p>Here's the work that I did so far:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  
  class Utility:
  
  @staticmethod
  def read_csv(csv, number_columns=[], categorical_columns=[], date_columns=[], drop_columns_if_empty=[], drop_duplicate_columns=[]):
      df = pd.read_csv(csv, na_values=['--', ''])
      df.rename(columns=lambda x: x.strip().replace('"', '').replace(' ', '_').replace('__', '_').lower(),
                inplace=True)
      df[number_columns] = df[number_columns].astype(str).replace({'[\$,)]': '', ' ': '', '[(]': '-'}, regex=True)
      for col in number_columns:
          df[col] = pd.to_numeric(df[col], errors='coerce')
      for col in date_columns:
          df[col] = pd.to_datetime(df[col], errors='coerce')
  
      df.dropna(subset=drop_columns_if_empty, how='any', inplace=True)
      df = df.applymap(lambda x: x.strip() if type(x) is str else x)
      if (len(drop_duplicate_columns) &gt; 1):
          df = df.drop_duplicates(drop_duplicate_columns, keep='last')
      for col in categorical_columns:
          df[col] = pd.Categorical(df[col])
  
      return df
  
  df = Utility.read_csv('http://opendata.toronto.ca/clerk/registry.service/death.csv', number_columns=['death_licenses'], categorical_columns=['place_of_death', 'civic_centre'], date_columns=['time_period'])
  df.sort_values(['time_period', 'civic_centre'], ascending=[True, False])
  df2 = df.groupby(['time_period', 'civic_centre'])['death_licenses'].agg('sum').reset_index()
  </code></pre>
  
  <p>I want to do something like this:
  <a href="https://i.stack.imgur.com/jYUYN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jYUYN.png" alt="grouped bar graph"></a></p>