20190527

matplotlibのpyplot.plotで地図を描く方法

20190526185456.png

地図を描くスタンダードな方法

mpl_toolkits.basemapというのが、スタンダード。
しかし、pyplotで何とか地図を描けないだろうか。

このサイトから地図データがダウンロードできる。

国土数値情報ダウンロードサービスで、
“N03-110331_28_EC01.shp”というのを、ダウンロードしてみた。

shapeファイルをプロットしてみた

これでできた。変数eは変換行列になっているので、いろいろ試すといいかもしれない。

必要なファイル。
カレントフォルダに、”N03-110331_28_EC01.shp”

windows 日本語使用のコード

#!/usr/bin/env python
# -*- coding: cp932 -*-

import time
from datetime import datetime as dt

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'IPAGothic'

import shapefile

#下の文字列に、シェイプファイルのアドレスを入力する。
sf = shapefile.Reader(r"N03-110331_28_EC01.shp",encoding='SHIFT-JIS') #,encoding='SHIFT-JIS'必須

f,(ax1) = plt.subplots(1,1)
e = [[1, 0], [0, 1]]#変換行列
a = 133.5

try:
i = 0
while True:
shape = sf.shape(i)
x = []
y = []
x2 = []
y2 = []
for i2 in range(len(shape.points)):
x.append(shape.points[i2][0])
y.append(shape.points[i2][1])
b = shape.points[i2]
c = [a, 0]
d = [b[0] - c[0], b[1] - c[1]]
#e = [[1, 0], [0, 1]]
f2 = np.dot(d, e)
c2 = [a, 0]
f2 = [f2[0] + c2[0], f2[1] + c2[1]]
x2.append(f2[0])
y2.append(f2[1])
#ax1.plot(x, y, color="k")
ax1.plot(x, y, color="k", linestyle="--")
ax1.plot(x2, y2, color="k", clip_on=False)
i += 1
except IndexError or UnicodeDecodeError or TypeError:
pass

ax1.set_aspect('equal')
ax1.grid(which="major", color="black", linestyle="--")
ax1.plot([a,a], [25,40], color="k", linestyle="dashdot" ,clip_on=False)

ax1.set_xlim(130, 137)
ax1.set_ylim(30, 37)

ax1.set_title(ur"兵庫県の地図")
ax1.set_xlabel(ur"経度(度)")
ax1.set_ylabel(ur"緯度(度)")
#plt.show()

tdatetime = dt.now()
itext = tdatetime.strftime('%Y%m%d'+ time.ctime()[11:13] + time.ctime()[14:16] + time.ctime()[17:19])
f.set_size_inches(19.2,10.8)

f.savefig("%s.png"%(itext))
posted by yuchan at 07:00 | Comment(0) | python
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント: