4 Replies - 411 Views - Last Post: 20 October 2013 - 06:44 PM Rate Topic: -----

#1 darealmzm  Icon User is offline

  • New D.I.C Head

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

AssertionError: Grouper and axis must be same length

Posted 20 October 2013 - 04:29 PM

What I'm trying to do is plot the path of 20 hurricanes using matplotlib, but every time I run my code I keep getting this error "Assertionerror: Grouper and axis must be same length". I'm a newb to python and matplotlib and I've already tried to google this error but I didn't have much luck as to what I'm doing wrong. Could someone please help me figure out this error or show me a better way of how to create this map using matplotlib. (I've attached the txt version of the csv file because I wasn't able to upload it)

Here's all the code I have so far:

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt 
from PIL import *
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')

# Opens data file

import pandas as pd
name = [ ]
df = pd.read_csv('louisianastormb.csv')
for name, group in df.groupby([name]):
    latitude = group.lat.values
    longitude = group.lon.values
    x,y = m(longitude, latitude)
    plt.plot(x,y,'y-',linewidth=2 )
    plt.xlabel('Longitude')
    plt.ylabel('Latitude')
    plt.title('20 Hurricanes with Landfall in Louisiana')



plt.savefig('20hurpaths.jpg', dpi=100)


Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: AssertionError: Grouper and axis must be same length

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,036
  • Joined: 12-December 12

Re: AssertionError: Grouper and axis must be same length

Posted 20 October 2013 - 04:43 PM

Post the full error message, which should include the line number(s) that generated the error.

BTW I don't know much about this but the error message seems quite clear:

Quote

Grouper and axis must be same length

Find out what grouper and axis refers to and make sure they have the same length - the same number of elements (I assume).

This post has been edited by andrewsw: 20 October 2013 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 darealmzm  Icon User is offline

  • New D.I.C Head

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

Re: AssertionError: Grouper and axis must be same length

Posted 20 October 2013 - 05:26 PM

Thanks for your quick response here is the full error message

Quote

Traceback (most recent call last):
File "/home/darealmzd/lstorms.py", line 31, in <module>
for name, group in df.groupby([name]):
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 186, in groupby
squeeze=squeeze)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 533, in groupby
return klass(obj, by, **kwds)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 197, in __init__
level=level, sort=sort)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 1325, in _get_grouper
ping = Grouping(group_axis, gpr, name=name, level=level, sort=sort)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 1129, in __init__
self.grouper = _convert_grouper(index, grouper)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 1350, in _convert_grouper
raise Assertionerror('Grouper and axis must be same length')
Assertionerror: Grouper and axis must be same length
>>>

This post has been edited by andrewsw: 20 October 2013 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,036
  • Joined: 12-December 12

Re: AssertionError: Grouper and axis must be same length

Posted 20 October 2013 - 05:39 PM

Did you work out what is meant by a 'grouper' and an 'axis'? And is line 31 the same as this line in your posted code?
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: AssertionError: Grouper and axis must be same length

Posted 20 October 2013 - 06:44 PM

View Postandrewsw, on 21 October 2013 - 12:39 AM, said:

Did you work out what is meant by a 'grouper' and an 'axis'? And is line 31 the same as this line in your posted code?

Sorry it took so long. Yes line 31 is the same in my posted code. The grouper is the groupby() that I used to try to group the data by name, however I can't figure out the problem with the axis.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1