matplotlib / basemap

Plot on map projections (with coastlines and political boundaries) using matplotlib
MIT License
780 stars 392 forks source link

Problem installing basemap via conda on Python 3.6: KeyError when finding pyproj library? #420

Closed fclubb closed 6 years ago

fclubb commented 6 years ago

When I try to install basemap via conda (Ubuntu 18.04) I get the following error:

from mpl_toolkits.basemap import Basemap
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/clubb/miniconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 146, in <module>
    pyproj_datadir = os.environ['PROJ_LIB']
  File "/home/clubb/miniconda3/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'PROJ_LIB'

I have the following packages installed:

basemap v1.1.0 conda-forge
geos 3.6.2 conda-forge
numpy 1.15.1 conda-forge
pyproj 1.9.5.1 conda-forge
pyshp 1.2.12 conda-forge

Any help on fixing would be much appreciated!

QuLogic commented 6 years ago

This is a conda-forge packaging problem and should be reported there. But also see the following other issue.

Duplicate of #419.

ocefpaf commented 6 years ago

Duplicate of #419.

Which is not a basemap issue, it is a conda usage question. The answer is here:

https://github.com/conda-forge/basemap-feedstock/issues/30

Hilbertvp commented 5 years ago

Hi all,

I know this is a duplicate thread, but I have been struggling this this issue for a good couple of hours (on windows), so I wanted to share my experiences so others can hopefully solve this a bit quicker.

For the conda version of basemap it needs the PROJ_LIB variable to be set so it can find the epsg data. For me the epsg data was packaged with the proj4 package.

For me the epsg data was located in the following folder: C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share

Therefore the following 2 lines in the python script that imports basemap solved the issue for me:

import os os.environ['PROJ_LIB'] = r'C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share'

I hope it helps.

Cheers

GangulyIITD commented 5 years ago

Hi all,

I know this is a duplicate thread, but I have been struggling this this issue for a good couple of hours (on windows), so I wanted to share my experiences so others can hopefully solve this a bit quicker.

For the conda version of basemap it needs the PROJ_LIB variable to be set so it can find the epsg data. For me the epsg data was packaged with the proj4 package.

For me the epsg data was located in the following folder: C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share

Therefore the following 2 lines in the python script that imports basemap solved the issue for me:

import os os.environ['PROJ_LIB'] = r'C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share'

I hope it helps.

Cheer

Yeah it helped. Thanks

rezapci commented 5 years ago

Hi all, I know this is a duplicate thread, but I have been struggling this this issue for a good couple of hours (on windows), so I wanted to share my experiences so others can hopefully solve this a bit quicker. For the conda version of basemap it needs the PROJ_LIB variable to be set so it can find the epsg data. For me the epsg data was packaged with the proj4 package. For me the epsg data was located in the following folder: C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share Therefore the following 2 lines in the python script that imports basemap solved the issue for me: import os os.environ['PROJ_LIB'] = r'C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share' I hope it helps. Cheer

Yeah it helped. Thanks

This is correct, it shouldn't happen, but its happening here and there

Asheef1447 commented 5 years ago

Hi all,

I know this is a duplicate thread, but I have been struggling this this issue for a good couple of hours (on windows), so I wanted to share my experiences so others can hopefully solve this a bit quicker.

For the conda version of basemap it needs the PROJ_LIB variable to be set so it can find the epsg data. For me the epsg data was packaged with the proj4 package.

For me the epsg data was located in the following folder: C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share

Therefore the following 2 lines in the python script that imports basemap solved the issue for me:

import os os.environ['PROJ_LIB'] = r'C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share'

I hope it helps.

Cheers

Thank you so much, this seems to work for me, i'm new in programming and i'd like to know whether i'd have to put the location ''C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share" in jupyter notebook cell every time when i use the package or there is permanent solution for it ?

Thank you.

ocefpaf commented 5 years ago

@rezapci and @Asheef1447 bare in mind that conda should take care of that. Maybe you are using the package via spyder or a non-activate jupyter instance and then changing the kernel another. You can always check if the environment variable is properly set with the command: echo %PROJ_LIB%. BTW, this works as expected:

conda create --name TEST basemao
conda activate TEST
python -c "from mpl_toolkits.basemap import Basemap"
echo %PROJ_LIB%
Mind-The-Data commented 5 years ago

Thanks @ocefpaf

Maybe you are using the package via spyder... then changing the kernel another.

anandguptakgp commented 3 years ago

pip install --upgrade --user matplotlib numpy pyproj pyshp OWSLib Pillow sudo apt-get update sudo apt install libgeos-dev pip install --user https://github.com/matplotlib/basemap/archive/master.zip

Try this it work smoothy System Note- Ananconda navigator , python 3.7 ubuntu 20.04

heatherpala commented 2 years ago

PLEASE HELP!!!! Trying to get basemap to work in Jupyter and getting this error: NameError Traceback (most recent call last) C:\Users\HEATHE~1\AppData\Local\Temp/ipykernel_14688/2506085524.py in 27 28 # create the map object, m ---> 29 m = Basemap(resolution='i', projection='cyl', \ 30 llcrnrlon=bot_left_lon, llcrnrlat=bot_left_lat, \ 31 urcrnrlon=top_right_lon, urcrnrlat=top_right_lat)

NameError: name 'Basemap' is not defined

with this code:

Simple Basemap examples

Example gallery: http://matplotlib.org/basemap/users/examples.html

Documentation: https://basemaptutorial.readthedocs.io/en/latest/

import os os.environ['PROJ_LIB'] = r'C:\Users\username\AppData\Local\Continuum\anaconda2\pkgs\proj4-4.9.2-vc10_0\Library\share' import matplotlib.pyplot as plt

from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon import numpy as np

Make the figure

fig = plt.figure() ax = fig.add_subplot(111)

Easiest way to make a basemap is to use the cylidrical projection and

define the bottom left lat/lon and top right lat/lon corners

Map of Utah

bot_left_lat =36.5 bot_left_lon =-114.5 top_right_lat =42.5 top_right_lon = -108.5

create the map object, m

m = Basemap(resolution='i', projection='cyl', \ llcrnrlon=bot_left_lon, llcrnrlat=bot_left_lat, \ urcrnrlon=top_right_lon, urcrnrlat=top_right_lat)

Note: You can define the resolution of the map you just created. Higher

resolutions take longer to create.

'c' - crude

'l' - low

'i' - intermediate

'h' - high

'f' - full

Draw some map elements on the map

m.drawcoastlines() m.drawstates() m.drawcountries() m.drawrivers(color='blue')

Drawing ArcGIS Basemap (only works with cylc projections??)

Examples of what each map looks like can be found here:

http://kbkb-wx-python.blogspot.com/2016/04/python-basemap-background-image-from.html

maps = ['ESRI_Imagery_World_2D', # 0 'ESRI_StreetMap_World_2D', # 1 'NatGeo_World_Map', # 2 'NGS_Topo_US_2D', # 3 'Ocean_Basemap', # 4 'USA_Topo_Maps', # 5 'World_Imagery', # 6 'World_Physical_Map', # 7 'World_Shaded_Relief', # 8 'World_Street_Map', # 9 'World_Terrain_Base', # 10 'World_Topo_Map' # 11 ] print ("drawing image from arcGIS server...",) m.arcgisimage(service=maps[8], xpixels=1000, verbose=False) print ("...finished")

Plot a scatter point at WBB on the map object

lon = -111.85 lat = 40.77 m.scatter(lon,lat,c='r',s=150)

Plot some wind barbs

lons = np.arange(-115,-100,.5) lats = np.arange(33,48,.5) u = np.arange(-5,10,.5) v = np.arange(5,20,.5) m.barbs(lons, lats, u, v, color='fuchsia')

Plot line between two points

(can also use greatcircle function to be more accurate)

x = [-110, -112] y = [40, 42] m.plot(x, y, color='navy', lw=5)

Fill two polygon shapes

patches = [] homeplate = np.array([[-114,38],[-113,37],[-112,38],[-112,40],[-114,40]]) patches.append(Polygon(homeplate)) triangle = np.array([[-111,38],[-110,37],[-110,42]]) patches.append(Polygon(triangle)) ax.add_collection(PatchCollection(patches, facecolor='lightgreen', edgecolor='k', linewidths=1.5))

Plot shapefiles: see here: http://basemaptutorial.readthedocs.io/en/latest/shapefile.html

Plot contours

m.contour(lons2D, lats2D, values2D) # contour lines

m.contourf(lons2D, lats2D, values2D) # contour color filled, can specify a cmap

Plot gridded data

m.pcolormesh(lons2D, lats2D, values2D) # can specify a cmap

Add plot title and other plot elements the normal way

plt.title('Map of Utah Basemap Example') plt.xlabel('this is the x label')

plt.show()