Looping through add_subplot

Hey all, going through one of the dataquest projects and the output of one of my codes is not what i expected. the correct data is there, just not formatted, so i’m thinking something is off with my loop statement. see details below. thanks!

Screen Link: https://app.dataquest.io/m/146/guided-project%3A-visualizing-earnings-based-on-college-majors/5/pandas-bar-plots

My Code:

cols = ['ShareWomen','Unemployment_rate']

for col in cols:
    fig = plt.figure(figsize=(12,5))
    ax1 = fig.add_subplot(1,2,1)
    ax2 = fig.add_subplot(1,2,2)
    ax1 = recent_grads.head(10).plot.bar(x='Major', y=str(col))
    ax2 = recent_grads.tail(10).plot.bar(x='Major', y=str(col))

What I expected to happen:
Expected to see a two 2column x 1row, one for ShareWomen and one for Unemployment.

What actually happened:
_got two empty 2column x 1row plots
_got 4 bar charts of the data I do want.

hey @Jhan

Welcome to DataQuest community!

you can try the following code and see the results:

recent_grads.head(10).plot.bar(x='major', y=col, ax = ax1) recent_grads.tail(10).plot.bar(x='major', y=col, ax = ax2)

In simplest terms, this is like assigning which subplot area, we want to display our created plot.

Hi @Jhan

I think you meant something like this:

cols = ['ShareWomen','Unemployment_rate']

def plotter(cols,*lines):
    for ln in lines:
        fig = plt.figure(figsize=(12,5))
        ax1 = fig.add_subplot(1,2,1)
        ax2 = fig.add_subplot(1,2,2)
        recent_grads.iloc[:int(ln)].plot.bar(x='Major', y=cols[0], ax = ax1)
        recent_grads.iloc[:int(ln)].plot.bar(x='Major', y=cols[1], ax = ax2)