FAQ: Different Plot Types - Histogram

This community-built FAQ covers the “Histogram” exercise from the lesson “Different Plot Types”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Visualization in Python

FAQs on the exercise Histogram

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Your histogram should have 10 bins, but your seems to have 10 bins.

I’m not sure what it wants from me…?

from matplotlib import pyplot as plt
from script import sales_times

#create the histogram here
plt.hist(sales_times, bins=9)

plt.show()

I’m trying to run this on my own but when I run your script it fails, I also saved the sales_times.csv.

# from matplotlib import pyplot as plt
import csv


def convert_time_to_num(time):
    mins = int(time[-2:])
    frac_of_hour = mins / 60.0
    hour = int(time[:-3])
    time = hour + frac_of_hour
    return time


sales_times_raw = []
with open("sales_times.csv") as csvDataFile:
    csvReader = csv.reader(csvDataFile)
    for row in csvReader:
        sales_times_raw.append(row[2])
sales_times_raw = sales_times_raw[1:]

sales_times = []
for time in sales_times_raw:
    sales_times.append(convert_time_to_num(time))

When I run this it spits out

Traceback (most recent call last):
  File "script.py", line 17, in <module>
    sales_times_raw.append(row[2])
IndexError: list index out of range       

Alternatively, I used Pandas to be able to get the list as such.

# import csv
import pandas as pd

sales = pd.read_csv("sales_times.csv")

def convert_time_to_num(time):
    mins = int(time[-2:])
    frac_of_hour = mins / 60.0
    hour = int(time[:-3])
    time = hour + frac_of_hour
    return time


# sales_times_raw = []
sales_times_raw = list(sales.time)
# with open("sales_times.csv") as csvDataFile:
#     csvReader = csv.reader(csvDataFile)
#     for row in csvReader:
#         sales_times_raw.append(row[])
# sales_times_raw = sales_times_raw[1:]

sales_times = []
for time in sales_times_raw:
    sales_times.append(convert_time_to_num(time))

Hi there,

Going through this exercise and curious at some of the lines written in the script.py file, specifically, lines 4 - 9:

def convert_time_to_num(time):
  mins = int(time[-2:])
  frac_of_hour = mins/60.0
  hour = int(time[:-3])
  time = hour + frac_of_hour
  return time

I’m curious as to how this function works. Maybe this is a result of me overthinking, as I’ve been staring at this for the last 30 min, but I can’t seem to figure out how this function converts time to numbers.

I’m not sure if I have the best grasp on slice notation, so any help on this would be appreciated.
To my understanding, the first command mins = int(time[-2:]) would mean anything after the second to last index (with the second to last digit included). Is this correct? For example, if the time were 12:30, “min” would have a value of 30. Another possibility could be 120 minutes, and in this case mean would have a value of 20.

Following that same logic, hour = int(time[:-3]) doesn’t make sense to me. Doesn’t this mean that everything except the last two items? So 12:30 would have a value of 1? Or 120 would have a value of 0?

I simply don’t understand the formula and can’t seem to grasp it.
This goes without saying, but I also don’t understand how adding hour + frac_of_hour calculates the time.

Any help on this would be greatly appreciated!