If it was matplotlib
If it's flourish
https://www.stat.go.jp/data/nihon/02.html
If you don't have a library for treemaps, `` `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
#Size and font settings
matplotlib.rcParams['figure.figsize'] = (16.0, 9.0)
plt.rcParams['font.family'] = 'Hiragino Sans'
plt.rcParams['font.weight'] = 'bold'
#Use ggplot style
style.use('ggplot')
#dataframe creation
population = [7369,3788,14360,21356,19476,22431,43248]
label = ["China(5.58%)","Shikoku(2.87%)","Kyushu-Okinawa\n(10.88%)","Chubu(16.18%)","Hokkaido and Tohoku(14.75%)","Kansai(16.99%)","Kanto(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'})
#Axis deletion
plt.axis('off')
#y-axis reverse
plt.gca().invert_yaxis()
#Title, position setting
plt.title("Japan's population ratio by region", fontsize=32,fontweight="bold")
ttl = ax.title
ttl.set_position([.5, 1.05])
#Background color
fig.set_facecolor('#eeffee')
When I do
It will be like this. It's kind of crap ...
So, let's try using the service flourish: https: //app.flourish.studio/ that can visualize nicely.
Please use a google account or something to register.
Select new project
Select treemap
You can create it with. The data used this time is as follows.
Nesting is set by region → prefecture. Of course, Size by specifies the estimated population for 2017. As you can see, you can use Japanese, and you can operate it in the same way as spreadsheet tools such as Excel.
I will repost the treemap using flourish. (The label disappears when you download it normally, so I'm taking a screenshot)
Recommended Posts