python - 如何使石斑鱼和轴的长度相同?

标签 python csv matplotlib pandas matplotlib-basemap

在我的作业中,我应该使用 matplotlib 在 map 上绘制 20 次飓风的轨迹。但是,当我运行代码时出现错误:AssertionError:Grouper and axis must be the same length

这是我的代码:

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)

这是完整的错误输出:

 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 

最佳答案

ValueError:Grouper 和轴的长度必须相同

如果您在 groupby 参数中使用双括号,就会发生这种情况。

(我发布这个是因为它是 Google 上的最高结果)。

关于python - 如何使石斑鱼和轴的长度相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483991/

相关文章:

ruby-on-rails - 将 Rails CSV 解析为哈希

python - 如何在 python 中使用 matplotlib 更改日期格式

python - pylab 与 opencv : returning completely different array values 中的 imread

python - 为什么 Beautiful Soup 会截断此页面?

python - 在 pdb 中打印值

python - 新数组的总大小必须不变

windows - 用于拆分 .csv 文件的批处理文件

python - cv2.Sobel 中的比例是什么意思?

python - 无法使用 Python 将阿拉伯语解码的 Unicode 保存到 CSV 文件

python - 在 Python Matplotlib 的 while 循环中生成时的数字 "not responding"