Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on basemap.drawcounties() Python 3.5.2 #324

Open
scollis opened this issue Oct 12, 2016 · 23 comments
Open

Fail on basemap.drawcounties() Python 3.5.2 #324

scollis opened this issue Oct 12, 2016 · 23 comments

Comments

@scollis
Copy link

scollis commented Oct 12, 2016

Platform:

Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux

Throws error on basemap.drawcounties() (over Illinois)

 Traceback (most recent call last):
  File "animate_aws.py", line 171, in <module>
    writer='imagemagick', fps=5)
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/matplotlib/animation.py", line 832, in save
    anim._init_draw()
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/matplotlib/animation.py", line 1221, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/matplotlib/animation.py", line 1243, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "animate_aws.py", line 166, in animate
    display.basemap.drawcounties()
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 1980, in drawcounties
    default_encoding='latin-1',drawbounds=drawbounds)
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 2146, in readshapefile
    for shprec in shf.shapeRecords():
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/shapefile.py", line 543, in shapeRecords
    for rec in zip(self.shapes(), self.records())]
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/shapefile.py", line 515, in records
    r = self.__record()
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/shapefile.py", line 491, in __record
    value = u(value)
  File "/home/scollis/anaconda/envs/pyart35/lib/python3.5/site-packages/mpl_toolkits/basemap/shapefile.py", line 58, in u
    return v.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 2: invalid continuation byte
@scollis
Copy link
Author

scollis commented Oct 12, 2016

>>> import matplotlib
>>> matplotlib.__version__
'1.5.3'
>>> 


@micahcochran
Copy link
Contributor

Does installing the version that is in the repository fix the problem?

@johnrobertlawson
Copy link

Having this problem too. Python 3.5.2 on matplotlib version 1.5.1 and 1.5.3 (tried both).

@WeatherGod
Copy link
Member

WeatherGod commented Jan 10, 2017 via email

@guziy
Copy link
Contributor

guziy commented Jan 10, 2017

Is it possible to have the failing code to try and reproduce the issue?

@johnrobertlawson
Copy link

johnrobertlawson commented Jan 10, 2017

I'm using Basemap version 1.0.7, which is the newest Anaconda will install under Python 3.5.2. (I notice there are newer Basemap versions.)

The code uses one of my own packages but you can get the idea with this example:

# W is a class instance that loads a WRF model netCDF datafile
# Hope it's fairly self-descriptive here

def create_basemap(W,ax=None):
    width_m = W.dx*(W.x_dim-1)
    height_m = W.dy*(W.y_dim-1)
    m = Basemap(projection='lcc',lon_0=W.cen_lon,lat_0=W.cen_lat,lat_1=W.truelat1,
            lat_2=W.truelat2,resolution='i',area_thresh=500,ax=ax,
            width=width_m,height=height_m,)
    mx, my = m(W.lons,W.lats)
    return m, mx, my

# data is a 2-D numpy array from that netCDF file

fig,axes = plt.subplots(4,4)
    mm,mx,my = create_basemap(W,ax=axes.flat[0])
    mm.contourf(mx,my,data)
    mm.drawcounties()

@guziy
Copy link
Contributor

guziy commented Jan 10, 2017

@johnrobertlawson I had to tweak your code for me but it seems there is no issue for basemap 1.0.8

Here is the quick notebook:

https://github.com/guziy/PyNotebooks/blob/master/basemap_demos/drawcoastlines_issue.ipynb

Cheers

@johnrobertlawson
Copy link

@guziy Thanks for verifying that. What's your Python version?

@guziy
Copy link
Contributor

guziy commented Jan 10, 2017

My python info:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

What worries me a bit here is that I do not see the plotted field... Are my projection params too off?

Cheers

@guziy
Copy link
Contributor

guziy commented Jan 10, 2017

Ah ok, now I see the field, had lons and lats in the wrong order in m.__call__

Cheers

@johnrobertlawson
Copy link

Turns out, after removing the data plotting (i.e. simply trying to plot a basemap), the counties still break the script. I tried to reproduce your exact code and it also breaks. This leaves just the 1.0.7 vs 1.0.8 version. I'm going to look into why conda update basemap doesn't go past 1.0.7.

@micahcochran
Copy link
Contributor

I'm going to look into why conda update basemap doesn't go past 1.0.7.

@johnrobertlawson
basemap version 1.0.8 has not been released. If you want to read more about that read issue #267.

@johnrobertlawson
Copy link

@micahcochran Good to know, thanks.

@WeatherGod
Copy link
Member

WeatherGod commented Jan 11, 2017 via email

@datacathy
Copy link

@WeatherGod I'm running basemap version 1.0.8, Python version 3.5.2, matplotlib 2.0.0. For me, drawcounties() doesn't give an error, it simply does nothing. I get no county boundaries drawn. Here is my code:

mm = Basemap(projection = 'merc', lat_0 = 42.0, lon_0 = -83.0, resolution = 'i', area_thresh = 0.1,
            llcrnrlon = -86.0, llcrnrlat = 39.0, urcrnrlon = -80.0, urcrnrlat = 45.0)
mm.drawmapboundary()
mm.fillcontinents(color = 'brown', lake_color = 'blue')
mm.drawcounties()
plt.show()

There is a similar problem on stackoverflow here but unfortunately it has no answer. Any ideas?

@WeatherGod
Copy link
Member

WeatherGod commented Feb 2, 2017 via email

@guziy
Copy link
Contributor

guziy commented Feb 2, 2017

@datacathy

It fills continents over your county boundaries (which is a bit weird)...
A workaround is to use mm.drawcounties(zorder=20)

Cheers

@WeatherGod
Copy link
Member

WeatherGod commented Feb 2, 2017 via email

@WeatherGod
Copy link
Member

WeatherGod commented Feb 2, 2017 via email

@datacathy
Copy link

@guziy it works, thanks for the workaround!
@WeatherGod thanks for looking into it.

@WeatherGod
Copy link
Member

WeatherGod commented Feb 2, 2017 via email

@Ackeraa
Copy link

Ackeraa commented Apr 3, 2019

I have the same problem. I've solved it.
I replace all "utf-8" with "latin-1" in shapefile.py, which located in ~/Library/Python/3.7/lib/python/site-packages/shapefile.py

@andersonbrito
Copy link

The suggestion given by @Ackeraa worked for me as well. 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants