Python folium で、都内の公園にまつわる情報を地図上に描画する | Monotalk の地図を描画中に気になりましたので、個別に調べてみました。調べた結果を記載します。
前提
以下環境で作業を実施しています。
-
OS
% sw_vers ProductName: Mac OS X ProductVersion: 10.12.6 BuildVersion: 16G29
-
pythonのverion
% python -V Python 2.7.10
-
使用したfolium のversion
% pip list | grep folium folium (0.5.0)
ドキュメントでの記載
folium — Folium 0.5.0+15.gecd5052 documentation に以下のように記載されています。
- fill_color (string, default ‘blue’) – Area fill color. Can pass a hex code, color name, or if you are binding data, one of the following color brewer palettes: ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’.
デフォルトはblue
で、以下のfill_colorが使用できるようです。
計12種類ですね。
- ‘BuGn’
- ‘BuPu’
- ‘GnBu’
- ‘OrRd’
- ‘PuBu’
- ‘PuBuGn’
- ‘PuRd’
- ‘RdPu’
- ‘YlGn’
- ‘YlGnBu’
- ‘YlOrBr’
- ‘YlOrRd’
実装上での記載
どのように実装されているのかを確認してみました。
folium/folium.py at master · python-visualization/folium で color_brewer
というメソッドで、カラーの処理を行っています。
if data is not None and not color_brewer(fill_color):
このメソッドは、folium.py内で定義されているものではなく、from branca.utilities import color_brewer
で、branca
というライブラリからimport して使っています。
branca
は、foliumの作成者の方が別で作成しているpython プロジェクトでした。
python-visualization/branca
こちらの対象メソッドが抜粋が以下になります。
* color_brewerの抜粋
scheme_info = {'BuGn': 'Sequential',
'BuPu': 'Sequential',
'GnBu': 'Sequential',
'OrRd': 'Sequential',
'PuBu': 'Sequential',
'PuBuGn': 'Sequential',
'PuRd': 'Sequential',
'RdPu': 'Sequential',
'YlGn': 'Sequential',
'YlGnBu': 'Sequential',
'YlOrBr': 'Sequential',
'YlOrRd': 'Sequential',
'BrBg': 'Diverging',
'PiYG': 'Diverging',
'PRGn': 'Diverging',
'PuOr': 'Diverging',
'RdBu': 'Diverging',
'RdGy': 'Diverging',
'RdYlBu': 'Diverging',
'RdYlGn': 'Diverging',
'Spectral': 'Diverging',
'Accent': 'Qualitative',
'Dark2': 'Qualitative',
'Paired': 'Qualitative',
'Pastel1': 'Qualitative',
'Pastel2': 'Qualitative',
'Set1': 'Qualitative',
'Set2': 'Qualitative',
'Set3': 'Qualitative',
}
colorbrewer
がなんなのかよくわかっていなかったので、調べてみたのですが、どうもカラーパレットと呼ばれるもののようです。 RColorBrewer |
ソースコードにリンクが記載されていますが、色の組み合わせは、ColorBrewer: Color Advice for Maps から確認することができます。
実際に使用できるか確認する
Document に記載されているカラー定義より、実装上の定義のほうが多かったので実際に指定できるかためしてみました。
テストに使用したスクリプトは以下にUPしてあります。
前回map出力に使用したスクリプトを少し修正したものになります。
folium_example/fill_color_test at master · kemsakurai/folium_example
-
threshold_scale を 6要素指定して実行
以下、スクリプトの抜粋です。
YlOrRd まで出力が成功しました。# 以下、threshold_scale を指定してcolorが出力できるか確認する m.choropleth(geo_data=data, data=df, columns=['A', 'AH'], key_on='feature.id', fill_color=color, threshold_scale=[0.9, 3.5, 7, 10, 15, 27], line_opacity=0.5, reset=True)
BrBg 以降は出力に失敗しています。BuGn :ok BuPu :ok GnBu :ok OrRd :ok PuBu :ok PuBuGn :ok PuRd :ok RdPu :ok YlGn :ok YlGnBu :ok YlOrBr :ok YlOrRd :ok BrBg :ng PiYG :ng PRGn :ng PuOr :ng RdBu :ng RdGy :ng RdYlBu :ng RdYlGn :ng Spectral :ng Accent :ng Dark2 :ng Paired :ng Pastel1 :ng Pastel2 :ng Set1 :ng Set2 :ng Set3 :ng
-
threshold_scale 未指定で実行
以下スクリプトの抜粋です。
Spectral まで出力に成功しました。# threshold_scale 未指定で実行 m.choropleth(geo_data=data, data=df, columns=['A', 'AH'], key_on='feature.id', fill_color=color, line_opacity=0.5, reset=True)
動作としては、threshold_scale 未指定だと、6色のColorスケールになるため、使用できるカラースキーマが増えるのかと思いました。BuGn :ok BuPu :ok GnBu :ok OrRd :ok PuBu :ok PuBuGn :ok PuRd :ok RdPu :ok YlGn :ok YlGnBu :ok YlOrBr :ok YlOrRd :ok BrBg :ok PiYG :ok PRGn :ok PuOr :ok RdBu :ok RdGy :ok RdYlBu :ok RdYlGn :ok Spectral :ok Accent :ng Dark2 :ng Paired :ng Pastel1 :ng Pastel2 :ng Set1 :ng Set2 :ng Set3 :ng
scheme_info
で、Qualitative と定義されているカラースキーマの出力がうまくいなかないようです。
実装上、以下のように、Qualitative
の場合は、6色で固定しないと出力できないような実装になっていましたので、threshold_scaleの指定を5にしてみます。1
6色固定と読み取ったのですが、スクリプトで、threshold_scale を4要素にしても問題なく動作しました。 -
color_brewerの抜粋
# Only if n is greater than six do we interpolate values. if n > 6: if core_color_code not in schemes: color_scheme = None else: # Check to make sure that it is not a qualitative scheme. if scheme_info[core_color_code] == 'Qualitative': raise ValueError("Expanded color support is not available" " for Qualitative schemes, restrict" " number of colors to 6")
-
threshold_scale 5つ指定して実行
以下はスクリプトの抜粋です。
こちらは全て成功しました。# threshold_scale 5つ指定して出力 m.choropleth(geo_data=data, data=df, columns=['A', 'AH'], key_on='feature.id', fill_color=color, threshold_scale=[0.9, 3.5, 7, 15, 27], line_opacity=0.5, reset=True)
カラースキーマのBuGn :ok BuPu :ok GnBu :ok OrRd :ok PuBu :ok PuBuGn :ok PuRd :ok RdPu :ok YlGn :ok YlGnBu :ok YlOrBr :ok YlOrRd :ok BrBg :ok PiYG :ok PRGn :ok PuOr :ok RdBu :ok RdGy :ok RdYlBu :ok RdYlGn :ok Spectral :ok Accent :ok Dark2 :ok Paired :ok Pastel1 :ok Pastel2 :ok Set1 :ok Set2 :ok Set3 :ok
Sequential
、Diverging
、Qualitative
についてですが、以下サイトに解説がありました。
ちずのつくりかた: 配色: 地理空間への招待状
まとめ
folium の コロプレス図の fill_color について調べてみました。以下まとめます。
-
ドキュメントの通り、連続値
Sequential
と呼ばれるカラースキーマである12のカラースキーマが使用できる。 -
threshold_scale を未指定(None)にすると、(発散型?)
Diverging
カラースキーマが使用できる。 -
threshold_scale を5以下で指定すると、
Qualitative
も含めて、全てのカラースキーマが使用できる。 -
GIS を触ったことなどなかったので、知らないことだらけで勉強にはなる。が、勉強ばかりであまり進捗しない。
以上です。
コメント