Basemap is a component of Matplolib, which is an important tool for map data visualization.

I have installed Anaconda based on Python3.6 and have tried many different ways to install basemap, which have failed several times. I have also searched many tips on the Internet and found this to be a common problem. So I decided to share this method, but there is no guarantee of success, for your reference.

First declare my PC environment:

Environment 1: Win10

  • Operating system :Windows10

  • Anaconda based on Python3.6

In windows10, the installation steps are as follows:

(1) First, download the BASemap and PyProJ WHL files from the following website

https://www.lfd.uci.edu/~gohlke/pythonlibs/

For python3.6, the following files are available on Windows:

  • Basemap 1.2.0 – cp36 – cp36m – win_amd64. WHL

  • Pyproj 1.9.5.1 – cp36 – cp36m – win_amd64. WHL

(2) Install pyProJ WHL file as follows:

Copy the code
  1. PIP install path + file name

Figure 1

(3) Install basemap WHL file as follows:

Figure 2

(4) Test whether basemAP is installed successfully

Copy the code
  1. from mpl_toolkits.basemap import Basemap

Figure 3

As shown above, it shows that Matplotlib is missing files and Basemap is not running correctly.

(5) Update Matplotlib library

Copy the code
  1. pip install matplotlib --upgrade

Figure 4.

(6) Test again whether Basemap is installed successfully

Figure 5

As you can see, Basemap is now working correctly.

Next, try running the code in the Jupyter Notebook.

Copy the code
  1. import matplotlib as mpl

  2. import matplotlib.pyplot as plt



  3. %matplotlib inline

  4. plt.style.use('ggplot')



  5. from mpl_toolkits.basemap import Basemap



  6. PLT. Figure (figsize = (8, 8))



  7. m = Basemap(projection='ortho', resolution=None,

  8. lat_0=50,lon_0=-100)

  9. M.b luemarble (scale = 0.5)

The running results are as follows:

Figure 6.

The above results indicate that Basemap has been successfully installed.

Environment 2: Win7

  • Operating system :Windows7

  • Anaconda based on Python3.6

I thought I was done installing Basemap, but I still have a Windows 7 PC.

Win7 and Win10 should be the same installation, but according to the above steps to install, occur the following error:

Run code:

Copy the code
  1. from mpl_toolkits.basemap import Basemap

The following problems occur:

Copy the code
  1. Traceback (most recent call last)

  2. <ipython-input-1-d9467465a3b6> in <module>()

  3. ----> 1 from mpl_toolkits.basemap import Basemap



  4. / opt/conda/lib/python3.6 / site - packages/mpl_toolkits/basemap/set py in < module > ()

  5. 144

  6. 145 # create dictionary that maps epsg codes to Basemap kwargs.

  7. --> 146 pyproj_datadir = os.environ['PROJ_LIB']

  8. 147 epsgf = open(os.path.join(pyproj_datadir,'epsg'))

  9. 148 epsg_dict={}



  10. / opt/conda/lib/python3.6 / OS. Py in the __getitem__ (self key)

  11. 667 except KeyError:

  12. 668 # raise KeyError with the original key value

  13. --> 669 raise KeyError(key) from None

  14. 670 return self.decodevalue(value)

  15. 671



  16. KeyError: 'PROJ_LIB'

I tried this “PROJ_LIB” problem several times and looked at the solutions offered by many websites, but found that most of them didn’t quite fit. Finally, one of the English solutions mentioned an indirect solution. After trying it, I found that Basemap could be installed and run successfully.

Here, share it with you.

Instead of describing the twists and turns of the trial-and-error process, the successful process is introduced (but not guaranteed to be problem-free on your computer).

The core of this approach is to create a virtual environment under Anaconda, and then install and run Basemap in the virtual environment.

1) Create a virtual environment under Anaconda

Open the Anaconda Prompt and create a virtual environment named ‘Py36’ in Anaconda’s default directory.

Copy the code
  1. Conda create -n py36 python=3.6

About the Anaconda under virtual environment installation, please refer to the following contents: http://liyangbit.com/anaconda/anaconda-install-env-to-custom-path/

Once installed, you can view the list of virtual environments with the following command:

Copy the code
  1. conda info --envs

Then activate the virtual environment

Copy the code
  1. activate py36

(2) Install Matplotlib in py36

Copy the code
  1. pip install matplotlib

(3) Install pyProJ WHL file in virtual environment “Py36” as follows:

Copy the code
  1. PIP install path + file name

(4) Install basemap WHL file in virtual environment “Py36” as follows:

Copy the code
  1. PIP install path + file name

(5) Test whether Basemap is successfully installed in virtual environment py36

Copy the code
  1. from mpl_toolkits.basemap import Basemap

Figure 7.

As you can see, Basemap works correctly in a virtual environment.

(6) Test whether Basemap can run in Jupyter Notebook

Program file directory to enter

Copy the code
  1. jupyter notebook

Found that the previously created virtual environment is not found by default.

In Jupyter Notebook, the created virtual environment requires a plug-in to be installed to display it correctly.

To open the Anaconda Prompt, install the plugin using the following command:

Copy the code
  1. conda install nb_conda

Restart The Jupyter Notebook and you can find the installed virtual environment as follows:

Figure 8.

Run the following code in a virtual environment:

Copy the code
  1. import matplotlib as mpl

  2. import matplotlib.pyplot as plt



  3. %matplotlib inline

  4. plt.style.use('ggplot')



  5. from mpl_toolkits.basemap import Basemap



  6. PLT. Figure (figsize = (8, 8))



  7. m = Basemap(projection='ortho', resolution=None,

  8. lat_0=50,lon_0=-100)

  9. M.b luemarble (scale = 0.5)

The running results are as follows:

Figure 6.

The above results indicate that Basemap has been successfully installed.


At this point, Basemap can be installed and run on both Windows 10 and 7.

If you are interested in Basemap, you can also install and use it.

If you have any questions, you may wish to communicate with them.


This article is from Lemonbit, a cloud community partner called “Python Data 101”. You can learn more about Python Data 101.