Draw a prefectural-level colored map like the one below in Python At the municipal level, go to here
By the way, the data used the number of passenger cars owned nationwide.
There is a [Code List](# Code List) at the end of the page.
Use a library called japanmap to create colored maps
pip install japanmap
Preparation of other libraries
import numpy as np
import pandas as pd
import cv2
from PIL import Image
import matplotlib.colors
import matplotlib.pyplot as plt
from japanmap import *
Read data The data was processed by downloading the 2019 Excel file from here.
df = pd.read_csv("how_many_cars.csv")
df = df.iloc[:53,:8]
Save the number of passenger cars by prefecture in dictionary format
for k,n in zip(df["Transport Bureau"], df["Passenger car"]):
if k in ["Sapporo", "Hakodate", "Asahikawa", "Muroran", "Kushiro", "Obihiro", "Kitami"]:
tmp=1
else:
tmp = pref_code(k)
tmp = pref_names[tmp]
#print(k,tmp)
if tmp not in num_dict:
num_dict[tmp] = n
else:
num_dict[tmp] += n
The contents of num_dict are still like this
num_dict
>> print(num_dict)
{'Mie Prefecture': 1161089.0,
'Kyoto': 1007847.0,
...
'Tottori prefecture': 346273.0,
'Kagoshima prefecture': 955360.0}
Convert the created num_dict value from the number of units to color information (RGB)
n_min = min(num_dict.values())
n_max = max(num_dict.values())
#print(n_min)
#print(n_max)
cmap = plt.cm.rainbow
norm = matplotlib.colors.Normalize(vmin=n_min, vmax=n_max)
def color_scale(r):
tmp = cmap(norm(r))
return (tmp[0]*255, tmp[1]*255, tmp[2]*255)
for k,v in num_dict.items():
num_dict[k] = color_scale(v)
Contents of the final num_dict
num_dict
>> print(num_dict)
{'Mie Prefecture': (19.5, 157.4059464288972, 241.021876181009),
'Kyoto': (41.49999999999999, 128.85792190698177, 246.1066417260737),
...
'Tottori prefecture': (127.5, 0.0, 255.0),
'Kagoshima prefecture': (47.5, 120.63885699318257, 247.29821868892742)}
Plot by passing num_dict to japanmap
plt.figure(figsize=(10,8))
plt.imshow(picture(num_dict))
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
plt.colorbar(sm)
plt.show()
that's all!
pip install japanmap
import numpy as np
import pandas as pd
import cv2
from PIL import Image
import matplotlib.colors
import matplotlib.pyplot as plt
from japanmap import *
df = pd.read_csv("how_many_cars.csv")
df = df.iloc[:53,:8]
num_dict={}
for k,n in zip(df["Transport Bureau"], df["Passenger car"]):
if k in ["Sapporo", "Hakodate", "Asahikawa", "Muroran", "Kushiro", "Obihiro", "Kitami"]:
tmp=1
else:
tmp = pref_code(k)
tmp = pref_names[tmp]
#print(k,tmp)
if tmp not in num_dict:
num_dict[tmp] = n
else:
num_dict[tmp] += n
n_min = min(num_dict.values())
n_max = max(num_dict.values())
#print(n_min)
#print(n_max)
cmap = plt.cm.rainbow
norm = matplotlib.colors.Normalize(vmin=n_min, vmax=n_max)
def color_scale(r):
tmp = cmap(norm(r))
return (tmp[0]*255, tmp[1]*255, tmp[2]*255)
for k,v in num_dict.items():
num_dict[k] = color_scale(v)
plt.figure(figsize=(10,8))
plt.imshow(picture(num_dict))
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
plt.colorbar(sm)
plt.show()
Recommended Posts