import matplotlib.pyplot as plt
# リスト4.8.1:円グラフの描画
plt.style.use("ggplot")
labels = ["自転車", "バス", "車"]
sizes = [25, 40, 35]
fig = plt.figure(figsize=(3, 3))
ax = fig.add_subplot(111)
# 円グラフの描画
ax.pie(sizes, labels=labels)
plt.show()
# リスト4.8.2:円グラフと軸の関係を描画
fig = plt.figure(figsize=(3, 3))
ax = fig.add_subplot(111)
ax.pie(sizes, labels=labels, radius=0.9, frame=True)
ax.text(-0.3, 0, "(0, 0)", fontsize=9)
plt.show()
# リスト4.8.3:円グラフの書式設定
# 円グラフの書式設定
fig = plt.figure(figsize=(3, 3))
ax = fig.add_subplot(111)
# 扇形の書式設定用辞書
wprops = {"edgecolor": "black", "linewidth": 2}
# テキストの書式設定用辞書
tprops = {"fontsize": 18}
ax.pie(
sizes,
explode=(0.0, 0.05, 0),
labels=labels,
autopct="%1.0f%%",
pctdistance=0.5,
shadow=False,
labeldistance=1.35,
startangle=90,
radius=0.3,
counterclock=False,
wedgeprops=wprops,
textprops=tprops,
center=(0.5, 0.5),
frame=True,
)
plt.show()
# リスト4.8.4:anime_genre_top10_pivoted.csvファイルの読み込み
import os
import pandas as pd
base_url = "https://raw.githubusercontent.com/practical-jupyter/sample-data/master/anime/"
anime_genre_top10_pivoted_csv = os.path.join(base_url, "anime_genre_top10_pivoted.csv")
df = pd.read_csv(anime_genre_top10_pivoted_csv, index_col="genre")
df
Movie | Music | ONA | OVA | Special | TV | |
---|---|---|---|---|---|---|
genre | ||||||
Comedy | 7293127.0 | 20860.0 | 1477266.0 | 5614758.0 | 6659293.0 | 65420862.0 |
Action | 10224960.0 | 77054.0 | 524907.0 | 5793680.0 | 3412689.0 | 63364032.0 |
Drama | 9034099.0 | 100734.0 | 188427.0 | 3043374.0 | 1915578.0 | 41011557.0 |
Romance | 5245386.0 | 42811.0 | 411331.0 | 3143167.0 | 2015820.0 | 40703388.0 |
Supernatural | 5452779.0 | 9189.0 | 192989.0 | 2696715.0 | 2336723.0 | 38956520.0 |
Fantasy | 8019406.0 | 43962.0 | 188937.0 | 2754224.0 | 2504131.0 | 34932563.0 |
Shounen | 5698808.0 | NaN | 97833.0 | 3861296.0 | 2591988.0 | 35532847.0 |
School | 1512533.0 | 5496.0 | 523223.0 | 2417660.0 | 2661425.0 | 35489099.0 |
Adventure | 9485223.0 | 42829.0 | 70431.0 | 2373765.0 | 2052024.0 | 27529975.0 |
Sci-Fi | 6967146.0 | 154801.0 | 415311.0 | 3358525.0 | 1616450.0 | 28072322.0 |
# リスト4.8.5:データの並べ替え
# TV
df_tv = df.sort_values(by="TV", ascending=False)["TV"]
# Movie
df_movie = df.sort_values(by="Movie", ascending=False)["Movie"]
df_tv
genre Comedy 65420862.0 Action 63364032.0 Drama 41011557.0 Romance 40703388.0 Supernatural 38956520.0 Shounen 35532847.0 School 35489099.0 Fantasy 34932563.0 Sci-Fi 28072322.0 Adventure 27529975.0 Name: TV, dtype: float64
# リスト4.8.6:TVとMovieとのジャンル内訳比較のための円グラフを描画
fig = plt.figure(figsize=(9, 4))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
# カラーセット
colors1 = (
"gold",
"coral",
"plum",
"orchid",
"lightseagreen",
"yellowgreen",
"lightskyblue",
"pink",
"cornflowerblue",
"orangered",
)
colors2 = (
"coral",
"orangered",
"plum",
"pink",
"gold",
"cornflowerblue",
"yellowgreen",
"lightseagreen",
"orchid",
"lightskyblue",
)
# TVの円グラフ
ax1.pie(
df_tv,
explode=(0, 0, 0, 0, 0, 0, 0.15, 0, 0, 0.15),
labels=df_tv.index,
autopct="%1.0f%%",
colors=colors1,
startangle=90,
counterclock=False,
)
# Movieの円グラフ
ax2.pie(
df_movie,
explode=(0, 0.15, 0, 0, 0, 0, 0, 0, 0, 0.15),
labels=df_movie.index,
autopct="%1.0f%%",
colors=colors2,
startangle=90,
counterclock=False,
)
ax1.set_title("TV")
ax2.set_title("Movie")
plt.subplots_adjust(wspace=0.3) # サブプロット間のスペース調整
plt.show()