Roller Coaster Project // Bar Graph Function // Trouble with Axis object

Hello all,

I am currently working through the “Roller Coaster” project in the Data Visualization segment of the Data Science Course. I’m having trouble with task #8. I am attempting to create a function that takes a park name and creates a bar graph that displays all the rides in that park and the # of inversions. I initially ran my code without trying to format the axis object. Once I included those three lines, my code is producing the following error:

TypeError: bar() missing 2 required positional arguments: 'left' and 'height'

Here is my code:

def pltBarInversionByPark(parkName,csv):
  dataFrame = pd.read_csv(csv)
  selectParkDf = dataFrame[dataFrame.park == parkName]
  
  rideList = []
  numInversions = []
  
  for coaster in selectParkDf.name.values.tolist():
    rideList.append(coaster)
  for num in selectParkDf.num_inversions.values.tolist():
    numInversions.append(num)
    
  #print(rideList)
  #print(numInversions)
  
  plt.bar(range(len(rideList)), numInversions)
  
  ax = plt.bar()
  ax.set_xticks(range(len(rideList)))
  ax.set_xticklabels(rideList)
  
  plt.show()

pltBarInversionByPark('Gardaland','roller_coasters.csv')

Thank you in advance!

Okay so… provide those arguments?
What are you missing? Do you not know what to provide, do you think you shouldn’t need to, do you think you already do, do you understand what the described problem is, have you read the described problem?

I guess all of the above? In all the lessons I’ve gone through on the subject i’ve never had to provide any arguments in the ax = plt.bar() line of code. Also, when I read the documentation on bar(), ‘left’ isn’t even listed as a parameter that it takes.

Do I provide them in the axis object? Do I provide them in the initial plt.bar() line?

Apologies if i’m not formulating these questions properly.

Your error message is incomplete, there is also a line number.

was it the same bar, and if so I still can’t answer it without being able to reproduce both versions (the code you’ve shown is incomplete and idk what the other one is at all)

…also, you’re doing exactly that in your other use of bar, so “never” doesn’t seem right

You can do:

help(plt.bar)

You can also look up the documentation for the thing that you’re using (how else would you figure out how you could at all use it?) https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html

Sidenote: if looking up documentation you’ll want to match the documentation version to your libary version

So you’d want to know what version of matplotlib you’re using

It might be able to tell you directly, maybe something like print(matplotlib.__version__), or you’d refer to how you installed it, maybe it was installed with pip and then you’d do pip show matplotlib:

import os

os.execv("/usr/bin/python3", ["python", "-m", "pip", "show", "matplotlib"])

Here is the entirety of my code:

import codecademylib3_seaborn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def pltBarInversionByPark(parkName,csv):
  dataFrame = pd.read_csv(csv)
  selectParkDf = dataFrame[dataFrame.park == parkName]
  
  rideList = []
  numInversions = []
  
  for coaster in selectParkDf.name.values.tolist():
    rideList.append(coaster)
  for num in selectParkDf.num_inversions.values.tolist():
    numInversions.append(num)
    
  #print(rideList)
  #print(numInversions)
  
  plt.bar(range(len(rideList)), numInversions)
  
  ax = plt.bar()
  ax.set_xticks(range(len(rideList)))
  ax.set_xticklabels(rideList)
  
  plt.show()

pltBarInversionByPark('Gardaland','roller_coasters.csv')

And here is the entirety of the error message:

Traceback (most recent call last):
  File "script.py", line 29, in <module>
    pltBarInversionByPark('Gardaland','roller_coasters.csv')
  File "script.py", line 23, in pltBarInversionByPark
    ax = plt.bar()
TypeError: bar() missing 2 required positional arguments: 'left' and 'height'

And yes never. During all of the lessons when plotting and visualizing data, I’ve never had to put anything in the parentheses when creating the axis object to label the axes. Whether it be ax = plt.subplot() or ax = plt.bar(). The parentheses have always stayed empty.

I read this as:

I did something similar before, looked the same, it’s slightly different

I could point you at that difference if you showed me some such code.

Just went to one of the lessons and copied my solution which worked:

import codecademylib
from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales =  [91, 76, 56, 66, 52, 27]

plt.bar(range(len(drinks)), sales)

#create your ax object here
ax = plt.bar()
ax.set_xticks(range(len(drinks)))
ax.set_xticklabels(drinks,rotation = 30)

plt.show()

Disregarding the first import which you are not using, that does not run successfully to end. That crashes.

I just re ran it in the lesson and it worked fine for me.
https://www.codecademy.com/paths/data-science/tracks/data-visualization/modules/dspath-matplotlib/lessons/matplotlib-ii/exercises/basic-bar-chart-ii

How are you measuring this?
If you remove all code after and including the bad call to plt.bar, do you still measure that as successful? This would suggest that your code doesn’t run to end.

Ok then is the lesson broken? Why are they teaching incorrect information? And can you help me learn what the lesson did not teach me?

Question, have you used Codecademy? You do know I run the code through the browser? If I click submit and the graph displays and they say “good job thats right!” then that is how I’m currently measuring success.

If you read plt.bar's documantation then after understanding what it does perhaps you can leverage it.

Do note that you’re using plt.bar elsewhere as well. Why use it twice?

I don’t know, because that is what they are teaching me to do?

It is reasonable to expect that a green success message means that something verified that work was done.

But your measurement should be looking at the work done. You should observe the effect that your code has, to see how changes affects the outcome and so on, that feedback is important.
That doesn’t excuse codecademy, obviously.

They probably didn’t suggest doing plt.bar(), I expect you misread something. But then you got feedback suggesting this was okay, so I can see why you might to do it. (Though, for what purpose did you do it? Everything you write has to have purpose, otherwise it should be removed!)

If I wanted to learn how to use matplotlib, I’d start here
https://matplotlib.org/tutorials/index.html
They explain a bit how things work in general and show how to get common tasks done and link to documentation so that you can look at how the things you care about behave.

Pardon the interruption, but I just tried:

ax = plt.bar()

in the Codecademy Learning Environment for the specified exercise, and as expected, an error is thrown:


@coreyh424, did you change your code after passing the step?

Here’s the hint for this step:
image

If I was to make a few guesses, then presumably code was run to generate an image which was then served on the web interface shown on the right side.
The image file is there, available to be served, and maybe that’s sufficient to produce a YAY too.
codecademy very often tests something other than the associated messages and test code can crash too and I’ve seen deliberate decisions about giving a success message if test code crashes to not punish the user, but that makes the user misled and hides the problem … so that plus the bubblegum that ties the different components together… it’s plausible no angry red showed up. It’s also easy to dismiss angry red messages if there’s other conflicting indications.