LoginSignup
4
2

More than 3 years have passed since last update.

PythonのMatplotlib Basemap Toolkitで日本地図を描く

Last updated at Posted at 2019-06-05

環境

  • Windows10 Pro
  • Python 3.6.4 :: Anaconda
  • matplotlib 3.0.3
  • basemap 1.2.0
  • basemap-data-hires 1.1.0
  • pyproj 1.9.5.1

概要

令和になったし、日本の地図を描きたくなることありますよね。
Basemapを使って地図を描いてみました。

エラーとその対処

basemapをimportする際に以下のようなエラーが出ました。

Traceback (most recent call last):
  File "basemap.py", line 2, in <module>
    from mpl_toolkits.basemap import Basemap
  File "C:\Users\jh_0606\Anaconda3\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 155, in <module>
    pyproj_datadir = os.environ['PROJ_LIB']
  File "C:\Users\jh_0606\Anaconda3\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'PROJ_LIB'

以下のように環境変数を設定してあげればOKです。

import os
os.environ['PROJ_LIB'] = "C:/Users/jh_0606/Anaconda3/Library/share"

コード

basemap.py
import os
os.environ['PROJ_LIB'] = "C:/Users/jh_0606/Anaconda3/Library/share"

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

fig=plt.figure()
ax=fig.add_axes([1,1,1,1])

#E120-E150, N20-N50, high-resolution, mercator_projection
m = Basemap(llcrnrlon=120.,llcrnrlat=20.,urcrnrlon=150.,urcrnrlat=50.,
            rsphere=(6378137.00,6356752.3142),
            resolution='h',projection='merc')

m.drawcoastlines()
m.fillcontinents()

# draw parallels
m.drawparallels(np.arange(10,50,10), labels=[1,1,0,1])
# draw meridians
m.drawmeridians(np.arange(120,150,10),labels=[1,1,0,1])

ax.set_title('Japan')
plt.show()

出力

basemap.png

素晴らしい。
今後、これを基本に拡張していきます。

Bibriography

Basemap 公式ドキュメント
https://matplotlib.org/basemap/index.html

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2