Search code examples
pythonpandasbar-chartcategorical-data

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!


Solution

  • 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)