Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introduction to Data Visualization with Matplotlib Chart Toppers Scatter Plot

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

TypeError: The Scatter Chart is not being generated

I'm trying to generate the scatter chart as shown in the video but my cell keeps generating a TypeError in Jupyter. I can't what's gone wrong. Line 17 should allow me to use the list in this way shouldn't it?

from itertools import groupby

import csv
import matplotlib.pyplot as plt

input_file = 'iris.csv'

with open(input_file, 'r') as iris_data:
    irises = list(csv.reader(iris_data))

colors = {"Iris-setosa": "#2B5B84", "Iris-versicolor": "g", "Iris-virginica": "purple"}

irises.pop()

for species, group in groupby(irises, lambda i: i[4]):
    categorised_irises = list(group)
    sepal_lengths = [float(irises[0]) for iris in categorised_irises]
    sepal_widths = [float(irises[1]) for iris in categorised_irises]
    plt.scatter(sepal_lengths, sepal_widths, s=10, c=colors[species], label=species)

plt.title('Fisher\'s Iris Data Set', fontsize=12)
plt.xlabel('sepal length (cm)', fontsize=10)
plt.ylabel('sepal width (cm)', fontsize=10)
plt.legend(loc='upper right')

plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-773bcecd5933> in <module>()
     15 for species, group in groupby(irises, lambda i: i[4]):
     16     categorised_irises = list(group)
---> 17     sepal_lengths = [float(irises[0]) for iris in categorised_irises]
     18     sepal_widths = [float(irises[1]) for iris in categorised_irises]
     19 plt.scatter(sepal_lengths, sepal_widths, s=10, c=colors[species], label=species)

<ipython-input-8-773bcecd5933> in <listcomp>(.0)
     15 for species, group in groupby(irises, lambda i: i[4]):
     16     categorised_irises = list(group)
---> 17     sepal_lengths = [float(irises[0]) for iris in categorised_irises]
     18     sepal_widths = [float(irises[1]) for iris in categorised_irises]
     19 plt.scatter(sepal_lengths, sepal_widths, s=10, c=colors[species], label=species)

TypeError: float() argument must be a string or a number, not 'list'

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Jonathan Grieve! It seems like you have a typo. The variable irises is a list which contains other lists. The variable you're looking for is iris.

You wrote:

sepal_lengths = [float(irises[0]) for iris in categorised_irises]

But I believe that should be:

sepal_lengths = [float(iris[0]) for iris in categorised_irises]

Hope this helps! :sparkles:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,705 Points

Thanks for your help with this. It's truly amazing what I don't see sometimes when I'm coding! :)

Hi Jennifer, if you have any clues as to my similar but different ValueError, they would be gladly appreciated!