libs.utils.graphutil
libs/utils/graphutil.py
1""" 2libs/utils/graphutil.py 3""" 4 5import logging 6 7import matplotlib.font_manager as fm 8import matplotlib.pyplot as plt 9import pandas as pd 10from matplotlib import use 11 12import libs.global_value as g 13 14 15def setup(): 16 """グラフ設定初期化""" 17 18 pd.options.plotting.backend = g.adapter.conf.plotting_backend 19 match g.adapter.conf.plotting_backend: 20 case "plotly": 21 return 22 23 plt.close() 24 use(backend="agg") 25 mlogger = logging.getLogger("matplotlib") 26 mlogger.setLevel(logging.WARNING) 27 28 # スタイルの適応 29 if (style := g.cfg.setting.graph_style) not in plt.style.available: 30 style = "ggplot" 31 plt.style.use(style) 32 33 # フォント再設定 34 for x in ("family", "serif", "sans-serif", "cursive", "fantasy", "monospace"): 35 if f"font.{x}" in plt.rcParams: 36 plt.rcParams[f"font.{x}"] = "" 37 38 fm.fontManager.addfont(g.cfg.setting.font_file) 39 font_prop = fm.FontProperties(fname=g.cfg.setting.font_file) 40 plt.rcParams["font.family"] = font_prop.get_name() 41 42 # グリッド線 43 if not plt.rcParams["axes.grid"]: 44 plt.rcParams["axes.grid"] = True 45 plt.rcParams["grid.alpha"] = 0.3 46 plt.rcParams["grid.linestyle"] = "--" 47 plt.rcParams["axes.axisbelow"] = True 48 49 50def gen_xlabel(game_count: int) -> str: 51 """X軸ラベル生成 52 53 Args: 54 game_count (int): ゲーム数 55 56 Returns: 57 str: X軸ラベル 58 """ 59 60 if g.params.get("target_count"): 61 xlabel = f"直近 {game_count} ゲーム" 62 else: 63 xlabel = f"集計日({game_count} ゲーム)" 64 match g.params.get("collection"): 65 case "daily": 66 xlabel = f"集計日({game_count} ゲーム)" 67 case "monthly": 68 xlabel = f"集計月({game_count} ゲーム)" 69 case "yearly": 70 xlabel = f"集計年({game_count} ゲーム)" 71 case "all": 72 xlabel = f"ゲーム数:{game_count} ゲーム" 73 case _: 74 if g.params.get("search_word"): 75 xlabel = f"ゲーム数:{game_count} ゲーム" 76 else: 77 xlabel = f"ゲーム終了日時({game_count} ゲーム)" 78 79 return xlabel 80 81 82def xticks_parameter(days_list: list) -> dict: 83 """X軸(xticks)に渡すパラメータを生成 84 85 Args: 86 days_list (list): 日付リスト 87 88 Returns: 89 dict: パラメータ 90 """ 91 92 days_list = [str(x).replace("-", "/") for x in days_list] 93 94 thresholds = [ 95 # データ数, 傾き, 位置 96 (3, 0, "center"), 97 (20, -30, "left"), 98 (40, -45, "left"), 99 (80, -60, "left"), 100 (float("inf"), -90, "center"), 101 ] 102 103 for limit, rotation, position in thresholds: 104 if len(days_list) <= limit: 105 break 106 107 return { 108 "ticks": list(range(len(days_list)))[::int(len(days_list) / 25) + 1], 109 "labels": days_list[::int(len(days_list) / 25) + 1], 110 "rotation": rotation, 111 "ha": position, 112 }
def
setup():
16def setup(): 17 """グラフ設定初期化""" 18 19 pd.options.plotting.backend = g.adapter.conf.plotting_backend 20 match g.adapter.conf.plotting_backend: 21 case "plotly": 22 return 23 24 plt.close() 25 use(backend="agg") 26 mlogger = logging.getLogger("matplotlib") 27 mlogger.setLevel(logging.WARNING) 28 29 # スタイルの適応 30 if (style := g.cfg.setting.graph_style) not in plt.style.available: 31 style = "ggplot" 32 plt.style.use(style) 33 34 # フォント再設定 35 for x in ("family", "serif", "sans-serif", "cursive", "fantasy", "monospace"): 36 if f"font.{x}" in plt.rcParams: 37 plt.rcParams[f"font.{x}"] = "" 38 39 fm.fontManager.addfont(g.cfg.setting.font_file) 40 font_prop = fm.FontProperties(fname=g.cfg.setting.font_file) 41 plt.rcParams["font.family"] = font_prop.get_name() 42 43 # グリッド線 44 if not plt.rcParams["axes.grid"]: 45 plt.rcParams["axes.grid"] = True 46 plt.rcParams["grid.alpha"] = 0.3 47 plt.rcParams["grid.linestyle"] = "--" 48 plt.rcParams["axes.axisbelow"] = True
グラフ設定初期化
def
gen_xlabel(game_count: int) -> str:
51def gen_xlabel(game_count: int) -> str: 52 """X軸ラベル生成 53 54 Args: 55 game_count (int): ゲーム数 56 57 Returns: 58 str: X軸ラベル 59 """ 60 61 if g.params.get("target_count"): 62 xlabel = f"直近 {game_count} ゲーム" 63 else: 64 xlabel = f"集計日({game_count} ゲーム)" 65 match g.params.get("collection"): 66 case "daily": 67 xlabel = f"集計日({game_count} ゲーム)" 68 case "monthly": 69 xlabel = f"集計月({game_count} ゲーム)" 70 case "yearly": 71 xlabel = f"集計年({game_count} ゲーム)" 72 case "all": 73 xlabel = f"ゲーム数:{game_count} ゲーム" 74 case _: 75 if g.params.get("search_word"): 76 xlabel = f"ゲーム数:{game_count} ゲーム" 77 else: 78 xlabel = f"ゲーム終了日時({game_count} ゲーム)" 79 80 return xlabel
X軸ラベル生成
Arguments:
- game_count (int): ゲーム数
Returns:
str: X軸ラベル
def
xticks_parameter(days_list: list) -> dict:
83def xticks_parameter(days_list: list) -> dict: 84 """X軸(xticks)に渡すパラメータを生成 85 86 Args: 87 days_list (list): 日付リスト 88 89 Returns: 90 dict: パラメータ 91 """ 92 93 days_list = [str(x).replace("-", "/") for x in days_list] 94 95 thresholds = [ 96 # データ数, 傾き, 位置 97 (3, 0, "center"), 98 (20, -30, "left"), 99 (40, -45, "left"), 100 (80, -60, "left"), 101 (float("inf"), -90, "center"), 102 ] 103 104 for limit, rotation, position in thresholds: 105 if len(days_list) <= limit: 106 break 107 108 return { 109 "ticks": list(range(len(days_list)))[::int(len(days_list) / 25) + 1], 110 "labels": days_list[::int(len(days_list) / 25) + 1], 111 "rotation": rotation, 112 "ha": position, 113 }
X軸(xticks)に渡すパラメータを生成
Arguments:
- days_list (list): 日付リスト
Returns:
dict: パラメータ