6
6

More than 3 years have passed since last update.

pythonでtreemap(面積グラフ)を作ったら微妙だったからflourishを使ったらまあまあいい感じになった

Posted at

つまり?

matplotlibだったら

treemap3.png

flourishだったら
スクリーンショット 2019-12-07 19.49.53.png

データの出典元

pythonで作る場合1

もしも、treemap用のライブラリ、squarifyが無ければ

pip3 install squarify
# lib
import pandas as pd
import squarify #Treemap Ploting
import matplotlib
from matplotlib import style
import matplotlib.pyplot as plt
import seaborn as sns

# Activate Seaborn
sns.set()
%matplotlib inline

# サイズとフォント設定
matplotlib.rcParams['figure.figsize'] = (16.0, 9.0)
plt.rcParams['font.family'] = 'Hiragino Sans'  
plt.rcParams['font.weight'] = 'bold'

# ggplot style使用
style.use('ggplot')

# dataframe 作成
population = [7369,3788,14360,21356,19476,22431,43248]
label = ["中国(5.58%)","四国(2.87%)","九州・沖縄\n(10.88%)","中部(16.18%)","北海道・東北(14.75%)","関西(16.99%)","関東(32.76%)"]
percentage = [5.58,2.87,10.88,16.18,14.75,16.99,32.76]
df = pd.DataFrame({"Population":population,"Label":label,"Percentage":percentage})

fig, ax = plt.subplots()
# Colormap
cmap = matplotlib.cm.Blues
# Min and Max Values
mini = min(df["Population"])
maxi = max(df["Population"])
# colors setting
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in df["Population"]]
# Plotting
squarify.plot(sizes=df["Population"], label=df["Label"], alpha=0.8, color=colors, text_kwargs={'fontsize':24,'color':'grey'})
# 軸削除
plt.axis('off')
# y軸逆に
plt.gca().invert_yaxis()
# タイトル、位置設定
plt.title("日本の地域別人口比率", fontsize=32,fontweight="bold")
ttl = ax.title
ttl.set_position([.5, 1.05])
# 背景色
fig.set_facecolor('#eeffee')

ってやると
treemap3.png

こうなります。
なんかダサいですよね…

そこで、いい感じの可視化が出来るサービスflourish:https://app.flourish.studio/ を使ってやってみます。

flourishを使う場合

登録は適当にgoogleアカウントとかつかってやってください。

  1. new projectを選択
    スクリーンショット 2019-12-07 20.07.16.png

  2. treemapを選択
    スクリーンショット 2019-12-07 20.08.02.png

とすると作成できます。
今回使ったデータでは以下のようにしています。
スクリーンショット 2019-12-07 20.13.00.png

Nestingは地域→都道府県で設定しています。
Size byはもちろん平成29年の推計人口を指定しています。
ご覧の通り日本語も使えますし、Excelなどの表計算ツールと同じような感じで操作できます。

flourishを使ったtreemapを再掲しておきます。
(普通にダウンロードするとラベルが消えてしまうのでスクリーンショットしています)
スクリーンショット 2019-12-07 19.49.53.png

6
6
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
6
6