CoderBC
CoderBC

Reputation: 1402

Plotting by the index in pandas

I have a data set that contains the colour of an item, the date and the number of colour picks. I stored it into a Data Frame like this:

             date    | picks |        
         ------------+-------|
colour
orange    2016-01-01 |     6 | 
          2016-01-01 |     4 | 
          2016-01-01 |    16 | 
black     2016-01-01 |     0 | 
          2016-01-02 |     7 | 
          2016-01-02 |     0 | 
 green    2016-01-02 |     8 | 
          2016-01-02 |     5 | 
          2016-01-03 |     4 | 

df = pd.DataFrame(
    {'colour': ['orange', 'orange', 'orange', 'black', 'black', 'black', 'green',
                'green', 'green'], 
     'date': ['2016-01-01', '2016-01-01', '2016-01-01', '2016-01-01', '2016-01-02',
              '2016-01-02', '2016-01-02', '2016-01-02', '2016-01-03'], 
     'picks': [6, 4, 16, 0, 7, 0, 8, 5, 4]})
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('colour')

I would like to plot subplots (picks vs. date) for each colour i.e. subplot by index. is there a way to do this?

This is what I have tried so far:

fig, axes=plt.subplot(1,3)
for subp in axes:
    df.plot(ax=sub,subplot=True)

But this is showing an error. I also tried this:

df.plot(ax=[axes[0,0],axes[0,1],axes[0,2],subplot=True)

This one works, however I want to know how can I iterate and do this instead of simply giving the parameters.

Upvotes: 0

Views: 493

Answers (2)

IanS
IanS

Reputation: 16251

Update: Just saw that you do not have a Multiindex series but a dataframe. In this case my answer will look a lot like tom's:

colors = df.index.unique()
f, axarr = plt.subplots(len(colors))
for idx, color in enumerate(colors):
    df.groupby(df.index).get_group(color).plot(ax=axarr[idx], x='date', y='picks')

Original answer below.


Would this work?

First I create some data:

iterables = [['orange', 'black', 'green'], [1, 2, 3, 4, 5]]
index = pd.MultiIndex.from_product(iterables, names=['color', 'date'])
s = pd.Series(np.random.randn(15), index=index)

Then I plot it:

colors = s.index.levels[0].tolist()
f, axarr = plt.subplots(len(colors), sharex=True)
for idx, color in enumerate(colors):
    s[color].plot(ax=axarr[idx])

Upvotes: 0

tmdavison
tmdavison

Reputation: 69213

You can use df.groupby(level=0) to split the dataframe up by the index value. You can iterate over this groupby object, and plot each group on a separate subplot.

For example:

import pandas a pd
import matplotlib.pyplot as plt

# This should reproduce your dataframe. 
# For simplicity, I replaced the dates with an integer to represent the day.
# That should be easy for you to change back.
df = pd.DataFrame([[1,6],[1,4],[1,16],[1,0],[2,7],[2,0],[2,8],[2,5],[3,4]],
                  index=['orange','orange','orange','black','black','black','green','green','green'],
                  columns=['date','picks'])

fig,axes = plt.subplots(1,3)

for a,(i,group) in enumerate(df.groupby(level=0)):
    print group
    gp = group.plot(ax=axes[a],x='date',y='picks')
    gp.set_title(i)

Which prints:

       date  picks
black     1      0
black     2      7
black     2      0
       date  picks
green     2      8
green     2      5
green     3      4
        date  picks
orange     1      6
orange     1      4
orange     1     16

And the plot looks like:

enter image description here

Upvotes: 1

Related Questions