Garret Raziel
Garret Raziel

Reputation: 385

How to draw stacked histogram in pandas

I have this data in pandas:

        fork  percentage_remains
0      True            20.000000
1      False            9.090909
2      False            2.000000
3      False            0.000000
4      False            0.000000
5      True            33.333333
6      False           20.000000
...

and I want to draw stacked histogram, where on x-axis would be percentage_remains, on y-axis would be degree (that means count of items in this bin) and it would be grouped by fork - two stacked histograms in the same plot where first histogram is for all values with fork == True and second histogram is for all values with fork == False. I am trying:

subset.plot(kind="hist", stacked=True, by="fork")

but it creates this image: wrong image and from labels and values in this histogram it seems that this isn't actually grouped by "fork" attribute.

Upvotes: 2

Views: 2453

Answers (1)

Bob Haffner
Bob Haffner

Reputation: 8483

What about something like this

import matplotlib.pyplot as plt
subset = pd.DataFrame({'fork': {0: True, 1: False, 2: False, 3: False, 4: False, 5: True, 6: False},
 'percentage_remains': {0: 20.0,
  1: 9.0909089999999999,
  2: 2.0,
  3: 0.0,
  4: 0.0,
  5: 33.333333000000003,
  6: 20.0}})

Filter for fork == True via boolean indexing

filter = subset["fork"] == True`

Then use matplotlib directly. Notice I'm passing a list, one element are the true values and the other is for the false values

    plt.hist([subset["percentage_remains"][filter],subset["percentage_remains"][~filter]],
                                                   stacked=True)
        plt.show()

enter image description here

Upvotes: 2

Related Questions