lowlyprogrammer
lowlyprogrammer

Reputation: 499

Make Bar Chart With Binned X Values Python

I want to make a bar chart that has binned values every 10 x values. Here is my bins array:bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] I do not want to use a histogram because I have specific y values that I want to use, not just the frequency of the binned values. I have a pandas dataframe with two columns: yardline_100 (these are the values that are becoming "binned", they always fall between 0 and 100) and epa. I want to have my yardline_100 on the x and epa on the y. How do I do this? plt.hist() only takes one argument for data. And I can't figure out how to make plt.bar() work with binned values. Advice?

Upvotes: 2

Views: 6656

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC, do you want something like this?

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

df = pd.DataFrame({'yardline_100':np.random.randint(0,100,200), 'epa':np.random.random(200)})
df['bin'] = pd.cut(df['yardline_100'], bins=range(0,101,10), labels=[f'{l}-{l+10}' for l in range(0,91,10)])

fig,ax = plt.subplots(2,2, figsize=(15,8))
ax=ax.flatten()
sns.stripplot(x='bin', y='epa', data=df, ax=ax[0])
sns.violinplot(x='bin', y='epa', data=df, ax=ax[1])
sns.boxplot(x='bin', y='epa', data=df, ax=ax[2])
sns.barplot(x='bin', y='epa', data=df, ax=ax[3])

Output:

enter image description here


Change bar width

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

df = pd.DataFrame({'yardline_100':np.random.randint(0,100,200), 'epa':np.random.random(200)})
df['bin'] = pd.cut(df['yardline_100'], bins=range(0,101,10), labels=[f'{l}-{l+10}' for l in range(0,91,10)])

fig,ax = plt.subplots(figsize=(15,8))

sns.barplot(x='bin', y='epa', data=df, ax=ax)

def change_width(ax, new_value) :
    for patch in ax.patches :
        current_width = patch.get_width()
        diff = current_width - new_value

        # we change the bar width
        patch.set_width(new_value)

        # we recenter the bar
        patch.set_x(patch.get_x() + diff * .5)

change_width(ax, 1.)

Output:

enter image description here

Upvotes: 3

Related Questions