0

I created a scatter plot on indian map in jupyter notebook but when i am trying to run the same code in my djnago app. It raises

ModuleNotFoundError: No module named 'mpl_toolkits.basemap'

Here's the code:

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

# make up some data for scatter plot
lats = np.random.randint(6, 37, size=50)
lons = np.random.randint(68, 97, size=50)

fig = plt.gcf()
fig.set_size_inches(8, 6.5)

m = Basemap(projection='cyl', \
            llcrnrlat=6., urcrnrlat=37., \
            llcrnrlon=68., urcrnrlon=97., \
            lat_ts=20, \
            resolution='c',epsg=3857)

m.bluemarble(scale=1)   
m.drawcoastlines(color='white', linewidth=0.2) 
m.drawmapboundary(fill_color='#D3D3D3')


x, y = m(lons, lats) 
plt.scatter(x, y, 10, marker='o', color='Red') 

plt.show()

I am using the same conda interpreter in my django app. whats is the reason for this error ?

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • Then you are using a different environment. – Stop harming Monica Jun 04 '19 at 11:45
  • How can I change the environment of pycharm to that of jupyter ? – Rahul Sharma Jun 04 '19 at 11:56
  • Well I don't know. I don't use neither pycharm nor conda and you are the one installing the interpreters and creating and choosing the environments. But the error message says that `basemap` is not properly installed for that particular combination of interpreter and environment, that is all I know. – Stop harming Monica Jun 04 '19 at 12:03
  • I have run your code in my Jupyter (without Django) and I've got the same error: `ModuleNotFoundError: No module named 'mpl_toolkits.basemap'`. Now I'm not shure wether something has changes with basemap or wether I have to install some additional packages for basemap. – pyano Jul 30 '19 at 12:58
  • @pyano I was unable to find a proper solution, If you do please share. – Rahul Sharma Jul 31 '19 at 04:58
  • You have to install basemap. Try that: https://stackoverflow.com/questions/52356192/modulenotfounderror-no-module-named-mpl-toolkits-basemap (it did not work for me - yet). And have a look here too: https://stackoverflow.com/questions/40374441/python-basemap-module-impossible-to-import – pyano Jul 31 '19 at 14:40

1 Answers1

0

I use Anaconda under Win10. With a different OS or installation the solution may be different for you, but I can try to sketch the path.

  1. You have to install Basemap. See here and here. For me conda install -c conda-forge basemap-data-hires did work.
  2. When importing from mpl_toolkits.basemap import Basemap I got an error: KeyError: PROJ_LIB. Following the experiences here you must find a directory share with epsg inside. It is expected to be in the conda or the anaconda directory.
  3. I did a search in my Anaconda3-dirctory and have found this path: "C:\Anaconda3\Library\share"
  4. Then the following code did work for me (and your code gives a nice picture too :-) :

    import os
    proj_lib = "C:\Anaconda3\Library\share"
    os.environ["PROJ_LIB"] = proj_lib
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    from mpl_toolkits.basemap import Basemap
    
    plt.figure(figsize=(8, 8))
    m = Basemap(projection='ortho', resolution=None, lat_0=50, lon_0=-100)
    m.bluemarble(scale=0.5);
    
pyano
  • 1,885
  • 10
  • 28