在Python 3.6.1中将某些投影与底图和Contourf一起使用时发生IndexError [英] IndexError when using some projections with basemap and contourf in Python 3.6.1

查看:182
本文介绍了在Python 3.6.1中将某些投影与底图和Contourf一起使用时发生IndexError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu 16.4上使用Python 3.6.1 64位,Qt 5.6.2,PyQt5 5.6,底图版本1.0.7,matplotlib 2.0.2.当我尝试将轮廓线底图结合使用时,将其与投影'cyl'配合使用,例如:

I'm using Python 3.6.1 64bits, Qt 5.6.2, PyQt5 5.6 on Ubuntu 16.4 with Basemap version 1.0.7, matplotlib 2.0.2. When I try to use a contourf with basemap, with projection 'cyl', such as:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

map = Basemap(projection='cyl',lat_0=45,lon_0=-100,resolution='c')

nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)

x, y = map(lons*180./np.pi, lats*180./np.pi)

cs = map.contour(x,y,wave+mean,15,linewidths=1.5)
plt.show()

我收到以下错误:

回溯(最近通话最近一次):

Traceback (most recent call last):

文件",第15行,在 cs = map.contour(x,y,wave + mean,15,linewidths = 1.5)

File "", line 15, in cs = map.contour(x,y,wave+mean,15,linewidths=1.5)

文件 "/home/gab/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py", 第521行,在with_transform中 返回plotfunc(self,x,y,data,* args,** kwargs)

File "/home/gab/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py", line 521, in with_transform return plotfunc(self,x,y,data,*args,**kwargs)

文件 "/home/gab/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py", 3542线,轮廓 xx = x [x.shape [0]/2,:]

File "/home/gab/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py", line 3542, in contour xx = x[x.shape[0]/2,:]

IndexError:仅整数,切片(:),省略号(...), numpy.newaxis(None)和整数或布尔数组是有效索引

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

例如,如果我使用投影正交",则不会发生此错误.我在Python 2.6上运行此代码没有问题.这个问题似乎与这个问题 ,但未得到回答.

This error does not happen if I use for instance the projection 'ortho'. I have no problem running this code on Python 2.6. This question seems to be the same as this one, which was not answered.

有什么想法吗?

推荐答案

对lanadaquenada的回应(我似乎无法评论)

In response to lanadaquenada (I cannot coment it seems)

根据Serenity的帖子,您实际上需要修改底图代码,而不是您的代码.底图很旧,不再受支持.它是在python 2.x发布时创建的,并且看起来它利用了python 2整数除法. Python 3现在可以正确"进行除法了,但是创建了一些旧代码来利用python 2的除法.

Based on Serenity's post you actually need to modify the basemap code, rather than your code. Basemap is old and not really supported anymore. It was created when python 2.x was released and it appears it utilised the python 2 integer division. Python 3 now does division "correctly", but some old code was created to take advantage of python 2 division.

使用python3和matplotlib 1.5.3时,我将收到有关此问题的警告,但这不是致命的.升级到matplotlib 2.0.2后,此错误变得致命,我的谷歌搜索导致了您的帖子.

While using python3 and matplotlib 1.5.3 I would receive a warning about this issue, but it was not fatal. After upgrading to matplotlib 2.0.2 this error became fatal and my googling lead to your post.

因此,根据Serenity的建议,您需要 使用

Therefore following Serenity's advice you need to Manually change lines with

xx[x.shape[0]/2, :]

xx[x.shape[0]//2, :] 

对我来说,这是path_where_your_python_libraries_are_installed/site-packages/mpl_toolkits/basemap/__init__.py

我正在使用底图版本1.0.7.

I am using basemap version 1.0.7.

从matplotlib 1.5.3过渡到版本2.0.2时,我需要进行此更改

I needed to make this change when transitioning from matplotlib 1.5.3 to version 2.0.2

这阻止了我的代码崩溃,并且我针对较旧的matplotlib版本进行的基本测试似乎可以产生正确的结果.

This stopped my code from crashing and my basic testing against older matplotlib version appears to produce the correct results.

我希望这不会在其他地方产生意想不到的后果,尽管底图是使用旧的整数除法设计的,所以我认为这是可以的

I hope this does not have unintended consequences else where, though basemap was designed with the old integer division so I assume it is OK

这篇关于在Python 3.6.1中将某些投影与底图和Contourf一起使用时发生IndexError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆