2

I have a Pandas data Series which I want to plot based on months like this one : enter image description here

Here is the code:

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

# Import text file which holds ImageStack's path
df = pd.read_csv('file.txt', header=None)
df.columns = ['paths']

# Columns 2=Year, 3=Month, 4=Day
df = df.ix[:, 1:4].astype(int)

df.columns = ['year', 'month', 'day']

count = df.groupby(['year', 'month']).count()
count.columns = ['count']

months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

nmonths = len(months)
fig, ax = plt.subplots()
ind = np.arange(nmonths)
width = 0.45

nyears = len(count.index.levels[0])
p = []
for year in count.index.levels[0]:
    df_empty = pd.DataFrame({'months': ind})
    monthly_sum = df_empty.join(count.ix[year, :].groupby(level=0)
                                .sum())['count'].values
    counts_until = df_empty.join(count.ix[:year, :].groupby(level=1)
                                 .sum())['count'].values
    p.append(ax.bar(ind, monthly_sum, width,
                    bottom=counts_until, alpha=0.8))

# Set x axis ticks and labels
ax.set_xticks(ind + width/2)
ax.set_xticklabels([months[i] for i in ind])

# Locate legend outside axes plot area
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend([pl[0] for pl in p], count.index.levels[0], loc='center left',
          bbox_to_anchor=(1, 0.5))

plt.show()

DATA

      year  month  day
0     2007      5    6
1     2007      5    6
2     2007      5    8
3     2007      5    8
4     2007      5   12
5     2007      5   15
6     2007      5   16
7     2007      5   19
8     2007      5   21
9     2007      5   21
10    2007      5   21
11    2007      5   24

But it does not give me a right Stacked bar chart ! I think the problem could be from counts_until is there someone can find a way to solve this problem !?

1
  • does not give me a right Stacked bar chart... what does it give you, and why is it wrong? Commented Nov 10, 2015 at 15:42

1 Answer 1

3

You can use directly the plot(kind='bar') method for this, if your data is in the appropriate format. With a dummy example:

In [81]: s = pd.Series(np.random.randint(2, size=2000), pd.date_range('2010-01-01', periods=2000))

In [82]: counts = s.groupby([s.index.year, s.index.month]).sum()

In [83]: counts
Out[83]:
2010  1     10
      2     22
      3     19
      4     16
      5     16
      6     16
      7     19
      8     14
...
2015  1     16
      2     16
      3     17
      4     15
      5     15
      6     11
dtype: int32

If you ensure the 'years' are different columns (eg with unstack), then you can specify stacked=True to say that the different columns should be stacked:

In [86]: counts.unstack(level=0).plot(kind='bar', stacked=True)
Out[86]: <matplotlib.axes._subplots.AxesSubplot at 0x10bfb4a8>

gives:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I want, Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.