matplotlib

【matplotlib】グラフの凡例のフォントサイズを変更する

MAX

凡例のハンドラーと文字列の両方のサイズ変更を行う。

本記事では、凡例の文字サイズの変更を行うが、フォントサイズの指定方法は色々な場所(凡例タイトル、グラフタイトル、x軸、y軸など)で共通の設定なので、指定方法を覚えておくと、グラフ表現の幅が広がる。

スポンサーリンク

サンプルデータ読み込み

seabornのtitanicをサンプルとして使用する。

1import numpy as np
2import pandas as pd
3
4import matplotlib.pyplot as plt
5import japanize_matplotlib
6import seaborn as sns
7
8%matplotlib inline
9
10df_titanic = sns.load_dataset("titanic")

特に何も設定せずに凡例を表示させると以下のようなグラフになる。

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定
6plt.legend()
7# グラフタイトル設定
8plt.title(f"legend_fontsize_default11")
9plt.tight_layout()
10plt.show()

フォントサイズを絶対値指定で変更

サイズの絶対値指定で変更

legendfontsizeに数値を指定することで、凡例のハンドラーと文字のサイズを指定できる。

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを指定。
6plt.legend(fontsize=14)
7# グラフタイトル設定
8plt.title(f"legend_fontsize_14")
9plt.tight_layout()
10plt.show()

フォントサイズを相対値指定で変更

fontsizeに「large」「small」などの文字を指定することで、デフォルトのサイズから相対的なサイズを指定できる。

指定可能な相対倍率

xx-small, x-small, small, medium, large, x-large, xx-largeが指定可能。

倍率は割と細かい。

1font_scalings = {
2    'xx-small': 0.579,
3    'x-small':  0.694,
4    'small':    0.833,
5    'medium':   1.0,
6    'large':    1.200,
7    'x-large':  1.440,
8    'xx-large': 1.728,
9    'larger':   1.2,
10    'smaller':  0.833,
11    None:       1.0,
12}

matplotlibのfont_scaliings設定箇所

xx-small指定(57.9%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="xx-small")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_xx_small")
9plt.tight_layout()
10plt.show()

x-small指定(69.4%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="x-small")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_x_small")
9plt.tight_layout()
10plt.show()

small指定(83.3%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="small")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_small")
9plt.tight_layout()
10plt.show()

medium指定(100%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="medium")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_medium")
9plt.tight_layout()
10plt.show()

large指定(120%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="large")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_large")
9plt.tight_layout()
10plt.show()

x-large指定(144%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="x-large")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_x_large")
9plt.tight_layout()
10plt.show()

xx-large指定(172.8%)

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定。サイズを相対指定。
6plt.legend(fontsize="xx-large")
7# グラフタイトル設定
8plt.title(f"legend_fontsize_xx_large")
9plt.tight_layout()
10plt.show()

まとめ

fontsizeを指定することで、凡例のハンドラーと文字のサイズを変更できる。

1# フォントサイズ:14pt
2plt.legend(fontsize=14)
3
4# フォントサイズ57.9%
5plt.legend(fontsize="xx-small")
6
7# フォントサイズ69.4%
8plt.legend(fontsize="x-small")
9
10# フォントサイズ83.3%
11plt.legend(fontsize="small")
12
13# フォントサイズ100%
14plt.legend(fontsize="medium")
15
16# フォントサイズ120%
17plt.legend(fontsize="large")
18
19# フォントサイズ144%
20plt.legend(fontsize="x-large")
21
22# フォントサイズ172.8%
23plt.legend(fontsize="xx-large")

色々な凡例の設定

グラフの凡例の位置を変更する

【matplotlib】グラフの凡例の位置を変更する【seaborn】
【matplotlib】グラフの凡例の位置を変更する【seaborn】

凡例の列数を変更する

【matplotlib】凡例の列数を変更する【Python】
【matplotlib】凡例の列数を変更する【Python】

凡例のハンドラーのサイズを変更する

【matplotlib】凡例のハンドラーのサイズを変更する【Python】
【matplotlib】凡例のハンドラーのサイズを変更する【Python】

凡例の余白を少なくする

【matplotlib】凡例の余白を少なくする【Python】
【matplotlib】凡例の余白を少なくする【Python】

凡例タイトルの大きさを変更する

【matplotlib】凡例タイトルの大きさを変更する【Python】
【matplotlib】凡例タイトルの大きさを変更する【Python】

FontPropertiesを使用した凡例タイトルの設定

【matplotlib】FontPropertiesを使用した凡例タイトルの設定【Python】
【matplotlib】FontPropertiesを使用した凡例タイトルの設定【Python】
スポンサーリンク
ABOUT ME
MAX
MAX
ITエンジニア、データサイエンティスト
新卒でSIerに入社し、フリーランスになってWEB系へ転向。
その後AIの世界へ足を踏み入れ、正社員に戻る。 テーブルデータの分析がメイン。
スポンサーリンク
記事URLをコピーしました
目次へ