6 Replies - 1456 Views - Last Post: 21 October 2013 - 04:33 PM Rate Topic: -----

#1 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

TypeError: unhashable type: 'list'

Posted 21 October 2013 - 01:04 AM

I'm trying to plot the path of 15 different storms on a map in 10 different colors. The color of the path should depend on the name of the storm. For example if the storm's name is ETHEL, the color of the storm's path should be red on the map. I've tried searching for this error, but there was nothing that came close to the problem I am having. Could some please help/point me in the right direction?

Here's the part of my code that keeps giving me the error in the title:

m.drawparallels(np.arange(10.,35.,5.),labels=[1,0,0,1])
m.drawmeridians(np.arange(-120.,-80.,5.),labels=[1,0,0,1])
m.drawmapboundary(fill_color='aqua')
color_dict = { 'ETHEL': 'white', 'BETSY': 'yellow', 'CAMILLE': 'green'}

colnames = ['Year','Name','Type','Latitude','Longitude']
data = pandas.read_csv('stormb.csv', names=colnames)
names = list(data.Name)
lat = list(data.Latitude)
long = list(data.Longitude)

lat.pop(0)
long.pop(0)
latitude= map(float, lat)
longitude = map(float, long)
x, y = m(latitude,longitude)
#Plots points on map
plt.plot(x,y,'y-',color=color_dict[names], linewidth=0.2 )
lg = pl.legend()
lg.get_frame().set_facecolor('grey')





Here's the error message that I keep getting is:
Traceback (most recent call last):                                                                                 
    File "/home/darealmzd/lstorms.py", line 51, in <module>                                                          
    plt.plot(x,y,'y-',color=colors[names], linewidth=2 )                                                           
   TypeError: unhashable type: 'list'                                                                                 
 >>> 




Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: unhashable type: 'list'

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,863
  • Joined: 16-October 07

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 04:28 AM

>>> color_dict = { 'ETHEL': 'white', 'BETSY': 'yellow', 'CAMILLE': 'green'}
>>> color_dict['BETSY']
'yellow'
>>> color_dict[['BETSY','ROSE']]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> 



Understand? You're using names, a list, as your lookup.

Now, if you looped through each of those names...
Was This Post Helpful? 1
  • +
  • -

#3 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 11:27 AM

Oh ok I see know thanks!!! I got rid of that error but now the points on my graph will not plot. Is there anyway to fix this?

This post has been edited by andrewsw: 21 October 2013 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,863
  • Joined: 16-October 07

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 11:33 AM

I'd need to see the whole program and a sample of the file to figure anything out, I'm afraid.
Was This Post Helpful? 0
  • +
  • -

#5 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 11:53 AM

View Postbaavgai, on 21 October 2013 - 06:33 PM, said:

I'd need to see the whole program and a sample of the file to figure anything out, I'm afraid.


Ok here's a sample of the file I'm using.
Year, Name, Type, Latitude, Longitude
1957,AUDREY,HU, 21.6, 93.3
1957,AUDREY,HU,22.0,  93.4
1957,AUDREY,HU,22.6,  93.5
1969,AUDREY,HU,28.2,99.6
1957,AUDREY,HU,26.5,93.8
1957,AUDREY,HU,27.9,93.8
1957,AUDREY,HU,29.3,95
1957,AUDREY,HU,27.9,93.8
1957,AUDREY,HU,29.3,93.8
1957,AUDREY,HU,30.7,93.5
1969,CAMILLE,HU, 21.6,99.3
1969,CAMILLE,HU,22.0,98.4
1969,CAMILLE,HU,22.6,90.5
1969,CAMILLE,HU,23.2,93.6



Here's my entire code:



import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import csv, os, scipy
import pandas
from PIL import *


data = np.loadtxt('louisianastormb.csv',dtype=np.str,delimiter=',',skiprows=1)
'''print data'''
fig = plt.figure(figsize=(12,12))

ax = fig.add_axes([0.1,0.1,0.8,0.8])

m = Basemap(llcrnrlon=-100.,llcrnrlat=0.,urcrnrlon=-20.,urcrnrlat=57.,
            projection='lcc',lat_1=20.,lat_2=40.,lon_0=-60.,
            resolution ='l',area_thresh=1000.)

m.bluemarble()
m.drawcoastlines(linewidth=0.5)
m.drawcountries(linewidth=0.5)
m.drawstates(linewidth=0.5)

# Creates parallels and meridians
m.drawparallels(np.arange(10.,35.,5.),labels=[1,0,0,1])
m.drawmeridians(np.arange(-120.,-80.,5.),labels=[1,0,0,1])
m.drawmapboundary(fill_color='aqua')
color_dict = {'AUDREY': 'red', 'ETHEL': 'white', 'BETSY': 'yellow','CAMILLE': 'blue', 'CARMEN': 'green',
'BABE': 'purple', 'BOB': '#ff69b4', 'FREDERIC': 'black', 'ELENA': 'cyan', 'JUAN': 'magenta', 'FLORENCE': '#faebd7',
'ANDREW': '#2e8b57', 'GEORGES': '#eeefff', 'ISIDORE': '#da70d6', 'IVAN': '#ff7f50', 'CINDY': '#cd853f',
'DENNIS': '#bc8f8f', 'RITA': '#5f9ea0', 'IDA': '#daa520'}

# Opens data file witn numpy
'''data = np.loadtxt('louisianastormb.csv',dtype=np.str,delimiter=',',skiprows=0)'''
'''print data'''
colnames = ['Year','Name','Type','Latitude','Longitude']
data = pandas.read_csv('louisianastormb.csv', names=colnames)
names = list(data.Name)
lat = list(data.Latitude)
long = list(data.Longitude)
colorName = list(data.Name)
#print lat
#print long
lat.pop(0)
long.pop(0)
colorName.pop(0)
latitude= map(float, lat)
longitude = map(float, long)
x, y = m(latitude,longitude)
#Plots points on map

for colorName in color_dict.keys():
    plt.plot(x,y,linestyle ='-',label=colorName,color=color_dict[colorName], linewidth=5 )

lg = plt.legend()
lg.get_frame().set_facecolor('grey')
plt.title('20 Hurricanes with Landfall in Louisiana')
plt.show()
plt.savefig('20hurricancepaths1.jpg', dpi=100)


Was This Post Helpful? 0
  • +
  • -

#6 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 02:06 PM

View Postbaavgai, on 21 October 2013 - 06:33 PM, said:

I'd need to see the whole program and a sample of the file to figure anything out, I'm afraid.

Ok I think I found out what the problem is. The lines are not plotting because I need a way to group the hurricanes coordinate by the name of the hurricane. Is there a way that I could group the coordinate together based on the name of the storm . For example, if in my sample file Audrey's coordinate are:
Year, Name, Type, Latitude, Longitude
1957,AUDREY,HU, 21.6, 93.3
1957,AUDREY,HU,22.0, 93.4
1957,AUDREY,HU,22.6, 93.5
1969,AUDREY,HU,28.2,99.6
1957,AUDREY,HU,26.5,93.8
1957,AUDREY,HU,27.9,93.8
1957,AUDREY,HU,29.3,95
1957,AUDREY,HU,27.9,93.8
1957,AUDREY,HU,29.3,93.8
1957,AUDREY,HU,30.7,93.5

Since I already have a color dictionary set up, how can I group the latitude and longitude values together based on the name of the hurricane then plot them?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,863
  • Joined: 16-October 07

Re: TypeError: unhashable type: 'list'

Posted 21 October 2013 - 04:33 PM

To be honest, I don't know what the hell pandas is or why you need it. Your data is simple, keep it that way.

With just csv:
def getData(filename):
    def parseRow(year, name, type, lat, lon):
        try:
            return int(year), name, type, float(lat), float(lon)
        except:
            return None
    with open(filename, 'r') as fh:
        reader = csv.reader(fh)
        data = [ parseRow(*x) for x in reader ]
    return [ row for row in data if row ]




Or, perhaps:
def getData(filename):
    data = { }
    with open(filename, 'r') as fh:
        reader = csv.reader(fh)
        for year, name, type, lat, lon in reader:
            try:
                item = (int(year), type, float(lat), float(lon))
                if not name in data:
                    data[name] = []
                data[name].append(item)
            except:
                pass
    return data
                
data = getData('louisianastormb.csv')                
pprint.pprint(data)



Results:
{'AUDREY': [(1957, 'HU', 21.6, 93.3),
            (1957, 'HU', 22.0, 93.4),
            (1957, 'HU', 22.6, 93.5),
            (1969, 'HU', 28.2, 99.6),
            (1957, 'HU', 26.5, 93.8),
            (1957, 'HU', 27.9, 93.8),
            (1957, 'HU', 29.3, 95.0),
            (1957, 'HU', 27.9, 93.8),
            (1957, 'HU', 29.3, 93.8),
            (1957, 'HU', 30.7, 93.5)],
 'CAMILLE': [(1969, 'HU', 21.6, 99.3),
             (1969, 'HU', 22.0, 98.4),
             (1969, 'HU', 22.6, 90.5),
             (1969, 'HU', 23.2, 93.6)]}



Similar methods could be used for other groupings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1