Can't understand the instructions of bar chart explanation

The below code is from the answer. I have some questions.

1, If we do import numpy as np, it means that we import all methods that numpy including, right? I try to do it, there is a error showing.
2,Any connection between fig,ax and ax.bar? i think the code should be axes.bar
3, I cant understand bar_heights = norm_reviews[num_cols].iloc[0].values, before we only learn df.iloc[] to select column or row. This time we do df[].iloc[] how to explain it?

Thank you so much!!

bar_heights = norm_reviews[num_cols].iloc[0].values

Screen Link:https://app.dataquest.io/m/144/bar-plots-and-scatter-plots/4/creating-bars

My Code:

import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']

bar_heights = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
fig, ax = plt.subplots()
ax.bar(bar_positions, bar_heights, 0.5)
plt.show()

Heya!

  1. Mind adding the error you’re seeing on your end for this? That code should work perfectly fine.
  2. plt.subplots() creates both a fig and axes object in one step — ax is the variable where the axes object is stored. ax.bar() simply plots on top of the axes object that was created
  3. It’s still df.iloc[] as you understand it — you’re simply trimming the df down to a smaller df with specific columns selected, then selecting the values in the first row of the new df
2 Likes

Thank you for your reply!!

Below is the error.
NameError: name ‘arange’ is not defined

I try to do fig=plt.sublots() then try fig.bar() but it did not work.Do i have to give the variable in
"name1,name2 " jsut like " fig,ax’’? I try to do fig=plt.subplots() and fig.bar(), but the error shows
“AttributeError: ‘tuple’ object has no attribute ‘bar’”.

I took a small dataframe from the dataset called “norm_reviews”, I tried norm_reviews.iloc[0]

The error shows
ValueError: incompatible sizes: argument ‘height’ must be length 5 or scalar
difficult to understand it.

Thank you!!

Yes, it enables us to access all functions in it. But to access a function, we have to use the following syntax np.function_name. If you only use function_name, you will receive such errors like the one you have received.

On the other hand, if you only import specific functions like:
from numpy import arange

then you can call the function_name without any issues.

Best,
Sahil

1 Like

plt.sublots() returns a tuple of two items:

(<matplotlib.figure.Figure at 0x7f15e6064748>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7f15e6064b70>)

As you can see, one is a Figure object, and the other one is an Axes object. If we call, .bar() method on the tuple object, we would get AttributeError: ‘tuple’ object has no attribute ‘bar’. This is why we are unpacking them to fig and ax variable. .bar() is a method of Axes object.

norm_reviews.iloc[0] returns an array of 6 values which is not compatible with the bar_positions we have specified arange(5) + 0.75 (an array of 5 items).

The length of bar_heights and bar_postions must be the same.

Best,
Sahil

2 Likes

Thank you!!! it is a clear answer!!!

2 Likes