[Python] Colored map plot at the city level

Purpose of this article

Draw a city-level colored map like the one below in Python At the prefectural level, go to here

plot08.png

At first, I was planning to use geopandas + geoplot, but I couldn't install geoplot well with Google Colab, so I gave up geoplot and made various mistakes to arrive at the simplest method. Basically it is OK if geopandas can be installed additionally.

By the way, the color of each color is based on the value of random numbers this time, so please change the data as appropriate.

There is a [Code List](# Code List) at the end of the page.

Commentary

Use a library called geopandas to create colored maps

pip install geopandas

Preparation of other libraries

import numpy as np
import geopandas as gpd
import random
import matplotlib.colors
import matplotlib.pyplot as plt

Download the data of the shape (polygon) of the city, ward, town, and village from "National land numerical information download service" and use appropriate editing software (QGIS, etc.) ) To perform preprocessing.

As shown below, the data that consists of multiple polygons must be combined into one even though they are in the same city, and the polygons must be simplified as necessary.

plot07.jpg When using QGIS, please refer to here.

Export the preprocessed data in geojson format and load it with geopandas I want you to change the file directory as appropriate.

filename = "/content/drive/My Drive/Colab Notebooks/pref47.geojson"
df = gpd.read_file(filename, encoding='SHIFT-JIS')

Also, add the data used for coloring to the geopandas format data frame here. The usage is the same as pandas, so it's easy to understand. This time, for the sake of simplicity, a random value is used, but it is assumed that data such as population and wages will be entered here.

df["target"] = [random.random() for i in range(df.shape[0])]

Define a function that determines the color to be painted from the data for each city, ward, town, and village.

def colors_scale(arr):
    n_min = min(arr)
    n_max = max(arr)
    cmap = plt.cm.rainbow
    norm = matplotlib.colors.Normalize(vmin=n_min, vmax=n_max)

    arr = [cmap(norm(r)) for r in arr]
    return arr, cmap, norm

Finally the map plot In the same way as pandas, only the data of interest is selected and used as mini_df. The point is to pass a list of color information with color = when doing df.plot

mini_df = df[df["N03_001"].isin(["Ehime Prefecture", "Tokushima Prefecture", "Kagawa Prefecture", "Kochi Prefecture"])]

num_color, cmap, norm = colors_scale(mini_df["num"])

mini_df.plot(color=num_color, figsize=(10,6))
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
plt.colorbar(sm)
plt.show()

that's all!

Code list

pip install geopandas
import numpy as np
import geopandas as gpd
import random
import matplotlib.colors
import matplotlib.pyplot as plt

filename = "/content/drive/My Drive/Colab Notebooks/pref47.geojson"
df = gpd.read_file(filename, encoding='SHIFT-JIS')

df["target"] = [random.random() for i in range(df.shape[0])]

def colors_scale(arr):
    n_min = min(arr)
    n_max = max(arr)
    cmap = plt.cm.rainbow
    norm = matplotlib.colors.Normalize(vmin=n_min, vmax=n_max)

    arr = [cmap(norm(r)) for r in arr]
    return arr, cmap, norm


mini_df = df[df["N03_001"].isin(["Ehime Prefecture", "Tokushima Prefecture", "Kagawa Prefecture", "Kochi Prefecture"])]

num_color, cmap, norm = colors_scale(mini_df["num"])

mini_df.plot(color=num_color, figsize=(10,6))
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
plt.colorbar(sm)
plt.show()

reference

National land numerical information download service 09. Combine several features into one feature Reading and drawing shapefiles and geojson using python

Recommended Posts

[Python] Colored map plot at the city level
[Python] Adjusted the color map standard
[Python] Import the module one level higher
Call the python debugger at any time
Python Basic Course (at the end of 15)
Compare the speed of Python append and map
Send Gmail at the end of the process [Python]
[Python] Display the Altair legend in the plot area
Spread the Geographical Survey Map tiles in Python
At the time of python update on ubuntu
Remove specific strings at the end of python
[Python] Violin Plot
python at docker
Plot and understand the multivariate normal distribution in Python
Let's look at the scatter plot before data analysis
Tasks at the start of a new python project