Matplotlib Styles Bar Graph Question

Screen Link:
https://app.dataquest.io/m/528/matplotlib-styles%3A-fivethirtyeight-case-study/3/fivethirtyeight-style

Hi,

Sorry I was not able to tag the course (358-3) to this topic post. I have a question regarding the creation of the bar chart in the Matplotlib Styles: FiveThirtyEight Case Study.

In this course, the instructions tell us to create two bar plots using the code below

style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize=(9, 5))
ax.barh(white_corr.index, white_corr, left=2, height=0.5)
ax.barh(red_corr.index, red_corr, height=0.5)
ax.grid(b=None, axis='both')
ax.set_xticklabels([])
ax.set_yticklabels([])
plt.show() 

My question is when do we know to plot the chart using just red_corr vs red_corr.index. I’ve played with the code by just entering red_corr, but can someone let me know the technical difference?

Thanks!

Best,
Anthony

Hi @anthony2013101, assuming I understand your question well, we need to use both red_corr and red_corr.index each time we create the bar plot with plt.barh() or ax.bar() because both functions require us to pass specific x- and y-coordinates when we call them.

If we use Series.plot.barh(), then we’re not required to specifically pass coordinates because this function is built to infer the x- and y-coordinates from the Series itself – the x-coordinates are the index values, and the y-coordinates are the column values. For this example, we’d only use red_corr.plot.barh().

I encourage you to check the documentation of all the three functions I mentioned.

3 Likes

@alex, yes you understood my question correctly. I think I had a brainfart, thank you for your answer! Will check the documentation for the three functions for review.

Hey, @anthony2013101, no probs, your question was legit, and any question goes when we’re learning.

1 Like