Paddy
Paddy

Reputation: 91

Stacked Bar Chart based on Pandas Column

I have a set of data which can be replicated with the following code:

import numpy as np

import pandas as pd

Neurons = np.array([
    20, 600, 300, 300, 200,  50,  20, 100,  50, 300, 100, 600, 20,20,
    600, 200, 200, 600, 600, 100, 200, 200, 300, 200, 100, 50, 100,
    600, 200, 300,  20,  20, 200, 300, 600, 600, 100, 300, 200, 300])

Option = np.array([
    'Put', 'Put', 'Put', 'Put', 'Put', 'Put', 'Put', 'Put',
    'Put', 'Put', 'Put', 'Put', 'Put', 'Put', 'Put', 'Put',
    'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call',
    'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call',
    'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call', 'Call'])

df = pd.DataFrame({"Neurons" : Neurons, "Option": Option})

The "Neurons" and "Option" column should be seen as categorical. I want to construct a bar chart whereby Neurons are shown in ascending order on the x-axis and # of Models on the y-axis.

For example there are 6 Models where there are 20 neurons, consisting of four Put and 2 Call. Thus, the bar chart should display two bars, with the y-axis reading 4 and 2 respectively, in two different colors and displayed in a legend. This should be repeated for all categorical values of Neurons. (There are six in total)

This is just a small subset of the data, where in the full dataset the Option column has up to six categorical options. I want a separate bar on the same graph for each.

Any help would be greatly appreciated. Thanking you in advance!

Upvotes: 1

Views: 252

Answers (2)

Paddy
Paddy

Reputation: 91

I am not sure if this is the most ideal solution to this problem, but after some more contemplation I did manage a workaround.

ct = pd.crosstab(df.Option,df.Neurons)
ct.transpose().plot.bar(stacked=False)

Upvotes: 1

bsauce
bsauce

Reputation: 644

Based on your comment, try this:

df.groupby('Option').Neurons.value_counts().unstack(0).plot.bar()

In this code, we are first grouping by Option, then getting value counts for each value of Neurons within the Option group. Then, unstack so that you have one column for Option="Put" and one column for Option="Call." Then, with the data in this format, you will get the desired plot.

Output: desired plot

Upvotes: 0

Related Questions