matplotlib

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

MAX

グラフ描画時の凡例のハンドラーの大きさ(縦、横)をそれぞれ変更する。

グラフによっては凡例のスペースを切り詰めたい時もあったりするため、凡例を省スペース化したい時などに使うかもしれない。

スポンサーリンク

サンプルデータの読み込み

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")

凡例ハンドラーの幅を変更する

plt.legendhandlelengthを指定することで変更可能。

単位はemでデフォルトは2となっている。

凡例ハンドラーの幅を短くする例

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

凡例ハンドラーの幅を長くする例

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

凡例ハンドラーの高さを変更する

plt.legendhandleheightを指定することで変更可能。

単位はemで、デフォルト値は0.7。

凡例ハンドラーの高さを低くする例

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

凡例ハンドラーの高さを高くする例

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

凡例ハンドラーとラベルの間隔を変更する

plt.legendにhandletextpadを指定することで、ハンドルとラベル(文字列)の間隔を変更可能。

単位はemでデフォルト値は0.8。

ハンドラーとラベルの間隔を短くする例

1# グラフのサイズ、背景色設定
2plt.figure(figsize=(6, 4), facecolor="w")
3# 棒グラフ作成
4sns.barplot(data=df_titanic, x="pclass", y="survived", hue="sex")
5# 凡例の設定
6# 凡例ハンドルとラベルの間隔を0.2emに設定
7plt.legend(handletextpad=0.2)
8# グラフタイトル設定
9plt.title(f"legend_handletextpad02")
10plt.tight_layout()
11plt.show()

ハンドラーとラベルの間隔を長くする例

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

まとめ

凡例を省スペース化したい時は大体これぐらいの設定が良い感じ。

1# 凡例ハンドルの幅を1emに設定
2# 凡例ハンドルの高さを0.5emに設定
3# 凡例ハンドルとラベルの間隔を0.2emに設定
4plt.legend(handlelength=1, handleheight=0.5, handletextpad=0.2)

色々な凡例の設定

凡例のその他の設定については以下の記事も参照

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

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

凡例の列数を変更する

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

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

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

凡例の余白を少なくする

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

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

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

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

【matplotlib】FontPropertiesを使用した凡例タイトルの設定【Python】
【matplotlib】FontPropertiesを使用した凡例タイトルの設定【Python】

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