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?
Mind adding the error youâre seeing on your end for this? That code should work perfectly fine.
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
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
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.
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.
(<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.