Cumulative histogram plot from dataframe

Viewed 662

The goal is to create a plot like this enter image description here

Dummy df:

columns = ['number_of_words', 'occurrences']
data = [[1, 2312252],
       [2,1000000],
       [3,800000],
        [4, 400000],
        [5, 100000],
        [6, 70000],
        [7, 40000],
        [8, 10000],
        [9, 4000],
        [10, 50]]
dummy_df = pd.DataFrame(columns=columns, data=data)

The y axis represents the occurrences and the x axis the number of words column from the dummy_df.

The x axis should be cumulative such that it stacks the values on top of each other.

Example: With number_of_words = 1 we have around 2.3 m occurrences. With number_of_words = 2 we have around 1m occurrences, thus it should plot 2.3m + 1m at occurrences = 2. At the final entry of number_of_words the histogram should reach sum(occurrences).

I do NOT want to normalize it.

2 Answers

Since you already got the frequencies worked out, just add it cumulatively:

dummy_df['acc'] = dummy_df.occurrences.cumsum()                                                                                                                                                                                       
ax = dummy_df['acc'].plot('bar', width=1, color='b')
dummy_df['acc'].shift().plot('bar', alpha=0.7, width=1, color='r', ax=ax)

To split it into parts, plot it twice. The first is the normal cumsum, then second is just the values, with the shifted cumsum setting the bottom (This overlaps the top of the previous plotted cumsum).

Using .iloc[1:] to slice the Series just before plotting removes the first bar, which you want to exclude.

fig, ax = plt.subplots()

df['occurrences'].cumsum().iloc[1:].plot(kind='bar', width=1, ec='k', ax=ax)
df['occurrences'].iloc[1:].plot(kind='bar', width=1, ec='k', 
                       bottom=df['occurrences'].cumsum().shift().fillna(0).iloc[1:], ax=ax, color='red')

plt.show()

enter image description here

Related