Function used for generating histograms

In the solution for Guided Project: Visualizing Earnings Based On College Majors, for the instruction on screen 3: Pandas, Histograms the following function is provided as possible solution:

Can someone please elaborate more on this function and how exactly does it work, especially last two lines of code. It is a bit unclear to me.
Many thanks.

Hi!
Let’s dissect the whole for loop to see what’s happening there.

for r in range(0,4): 

you are going to iterate for each value of r: 0, 1, 2, 3. Four iteration in total.

For each iteration:

  1. you are going to add a subplot on a grid consisting of 4 rows and 1 column in the position defined by value of r
ax = fig.add_subplot(4, 1, r+1)
  1. Plot a histogram on the subplot added in step 1. The data for the histogram will correspond to the column name with index r from the list cols defined in the primer line of your code.
ax = recent_grads[cols[r]].plot(kind='hist', rot=30)

If you have doubts on how the .add_subplot() method works, you can check the mission on multiple plots (https://app.dataquest.io/m/143/multiple-plots/1/recap).

Please, check also the guides on formatting a question for the convenience of all members of the Community: Guidelines for asking a technical question in our Community, which include the recommendation to provide all the code as the markdown and not a screenshot and also include the URL link to the mission you have your doubts about.

1 Like

Ksenia, many thanks for clarifying. And also thanks for pointing out to guidelines for asking questions.

1 Like