各種目盛りフォーマッタ

関連: 目盛りフォーマット設定・取得

メモ

各種目盛りフォーマッタ 一覧

クラス
ticker.Formatter【フォーマッタ (基本)】
EngFormatter【フォーマッタ (工学)】3.1
FixedFormatter【フォーマッタ (固定)】
FormatStrFormatter【フォーマッタ (旧フォーマット文字列)】
FuncFormatter【フォーマッタ (ユーザー定義関数)】
IndexDateFormatter 3.33.5
IndexFormatter 3.33.5
LogFormatter【対数フォーマッタ (基本)】
LogFormatterExponent【対数フォーマッタ (指数)】
LogFormatterMathtext
LogFormatterSciNotation
LogitFormatter
NullFormatter【フォーマッタ (ヌル)】
OldScalarFormatter 3.33.5
PercentFormatter【フォーマッタ (パーセント)】
ScalarFormatter【フォーマッタ (スカラー)】3.1 / 3.3 / 3.4
StrMethodFormatter【フォーマッタ (新フォーマット文字列)】
dates.AutoDateFormatter【日付フォーマッタ (自動)】
ConciseDateFormatter【日付フォーマッタ (簡潔)】3.1
DateFormatter【日付フォーマッタ】
category.StrCategoryFormatter

Formatter【フォーマッタ (基本)】

メモ

  • 各種フォーマッタの親クラス

構文

class matplotlib.ticker.Formatter( )
属性備考
locs(デフォルト:[ ])
メソッド備考
__call__(x, pos=None)継承クラスでオーバーライド
static fix_minus(s)
format_data(value)
format_data_short(value)
format_ticks(values) 3.1
get_offset()
set_locs(locs)

EngFormatter【フォーマッタ (工学)】3.1

メモ

  • 工学フォーマッタ
    • 1000 の累乗をプレフィックスと単位で表現 (ENG_PREFIXES 定義)
      10Nプレフィックス
      -24'y'
      -21'z'
      -18'a'
      -15'f'
      -12'p'
      -9'n'
      -6'µ'
      -3'm'
      0''
      3'k'
      6'M'
      9'G'
      12'T'
      15'P'
      18'E'
      21'Z'
      24'Y'

構文

class matplotlib.ticker.EngFormatter(unit='', places=None, sep=' ', *3.1, usetex3.1 =None, useMathText3.1 =None)

unit (str)単位記号 ('Hz'・'m' 等)
places (int)小数点以下の桁数 (1 ~ 3 / 省略:0~5 自動)
sep (str値と単位の間のセパレータ (以下の指定可)
    '' (空文字列):セパレータなし
    '\N{THIN SPACE}':細いスペース (U+2009)
    '\N{NARROW NO-BREAK SPACE}':細い改行なしスペース (U+202F)
    '\N{NO-BREAK SPACE}':改行なしスペース (U+00A0)
以下キーワード引数 3.1
usetex (bool)3.1TeX 数学モードの使用有無 (デフォルト:False rcParams["text.usetex"])
useMathText (bool)3.1数学フォーマットの使用有無 (デフォルト:False rcParams["axes.formatter.use_mathtext"])
属性備考
ENG_PREFIXESプレフィックス (メモ参照)
useMathText
usetex
メソッド備考
format_eng(num)
get_useMathText()
get_useMathText()
set_useMathText(val)
set_usetex(val)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1000, 2000, 3000]
y = [0, 3_000_000, 1_000_000, 2_000_000]

# グラフ
ax.set_title('EngFormatter【フォーマッタ (工学)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([500, 1500, 2500], minor=True)
ax.xaxis.set_major_formatter(ptick.EngFormatter('m'))
ax.xaxis.set_minor_formatter(ptick.EngFormatter('\n(m)', sep=''))
ax.set_yticks(y)
ax.set_yticks([500_000, 1_500_000, 2_500_000], minor=True)
ax.yaxis.set_major_formatter(ptick.EngFormatter('Hz'))
ax.yaxis.set_minor_formatter(ptick.EngFormatter('(Hz)', sep=''))

# 表示
plt.show()

EngFormatter【工学フォーマッタ】のサンプル画像


FixedFormatter【フォーマッタ (固定)】

メモ

  • 固定フォーマッタ
    • ticker.FixedLocator の使用が必要

構文

class matplotlib.ticker.FixedFormatter(seq)

seqラベル文字列のシーケンス
メソッド備考
get_offset()
set_offset_string(ofs)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('FixedFormatter【フォーマッタ (固定)】')
ax.plot(x, y)
ax.xaxis.set_major_locator(ptick.FixedLocator([0, 1, 2, 3]))
ax.xaxis.set_minor_locator(ptick.FixedLocator([0.5, 1.5, 2.5]))
ax.xaxis.set_major_formatter(ptick.FixedFormatter(['X0', 'X1', 'X2', 'X3']))
ax.xaxis.set_minor_formatter(ptick.FixedFormatter(['xa', 'xb', 'xc']))
ax.yaxis.set_major_locator(ptick.FixedLocator([0, 1, 2, 3]))
ax.yaxis.set_minor_locator(ptick.FixedLocator([0.5, 1.5, 2.5]))
ax.yaxis.set_major_formatter(ptick.FixedFormatter(['Y0', 'Y1', 'Y2', 'Y3']))
ax.yaxis.set_minor_formatter(ptick.FixedFormatter(['ya', 'yb', 'yc']))

# 表示
plt.show()

FixedFormatter【フォーマッタ (固定)】のサンプル画像


FormatStrFormatter【フォーマッタ (旧フォーマット文字列)】

メモ

  • 旧フォーマット文字列指定のフォーマッタ

構文

class matplotlib.ticker.FormatStrFormatter(fmt)

fmtフォーマット文字列 (旧スタイル:値に対して % 使用)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('FormatStrFormatter\n【フォーマッタ (旧フォーマット文字列)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([0.5, 1.5, 2.5], minor=True)
ax.xaxis.set_major_formatter(ptick.FormatStrFormatter('%.1f'))
ax.xaxis.set_minor_formatter(ptick.FormatStrFormatter('(%.2f)'))
ax.set_yticks(y)
ax.set_yticks([0.5, 1.5, 2.5], minor=True)
ax.yaxis.set_major_formatter(ptick.FormatStrFormatter('%.1e'))
ax.yaxis.set_minor_formatter(ptick.FormatStrFormatter('(%.2e)'))

# 表示
plt.show()

FormatStrFormatter【フォーマッタ (旧フォーマット文字列)】のサンプル画像


FuncFormatter【フォーマッタ (ユーザー定義関数)】

メモ

  • ユーザー定義関数指定のフォーマッタ

構文

class matplotlib.ticker.FuncFormatter(func)

funcユーザー定義関数 (下記参照)
引数備考
x目盛り値
pos位置
戻り値
目盛りラベル
メソッド備考
get_offset()
set_offset_string(ofs)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

def x_major_formatter(x, pos):
    if x == 0:
        label = '0'
    else:
        label = f'{x:.1f}'
    return label

def x_minor_formatter(x, pos):
    label = f'({x:.2f})'
    return label

def y_major_formatter(y, pos):
    if y == 0:
        label = '0'
    else:
        label = f'{y:.1e}'
    return label

def y_minor_formatter(y, pos):
    label = f'({y:.2e})'
    return label


# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('FuncFormatter\n【フォーマッタ (ユーザー定義関数)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([0.5, 1.5, 2.5], minor=True)
ax.xaxis.set_major_formatter(ptick.FuncFormatter(x_major_formatter))
ax.xaxis.set_minor_formatter(ptick.FuncFormatter(x_minor_formatter))
ax.set_yticks(y)
ax.set_yticks([0.5, 1.5, 2.5], minor=True)
ax.yaxis.set_major_formatter(ptick.FuncFormatter(y_major_formatter))
ax.yaxis.set_minor_formatter(ptick.FuncFormatter(y_minor_formatter))

# 表示
plt.show()

FuncFormatter【フォーマッタ (ユーザー定義関数)】のサンプル画像


NullFormatter【フォーマッタ (ヌル)】

メモ

  • ヌルフォーマッタ
    • 空文字列を返却する為、目盛りなし

構文

class matplotlib.ticker.NullFormatter( )

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('NullFormatter【フォーマッタ (ヌル)】')
ax.plot(x, y)
ax.xaxis.set_major_formatter(ptick.NullFormatter())
ax.yaxis.set_major_formatter(ptick.NullFormatter())

# 表示
plt.show()

NullFormatter【フォーマッタ (ヌル)】のサンプル画像


PercentFormatter【フォーマッタ (パーセント)】

メモ

  • パーセント フォーマッタ
    • パーセンテージ (%) で表現

構文

class matplotlib.ticker.PercentFormatter(xmax=100, decimals=None, symbol='%', is_latex=False)

xmax (float)100% に対応するデータ値
decimals (None | int)小数点以下の桁数 (省略:自動設定)
symbol (str | None)パーセンテージのシンボル
is_latex (bool)LaTeXの使用有無 (rcParams["text.usetex"] Falsesymbol (シンボル)内のLaTeX予約文字をエスケープ)
属性備考
symbol
メソッド備考
convert_to_pct(x)
format_pct(x, display_range)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('PercentFormatter\n【フォーマッタ (パーセント)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([0.5, 1.5, 2.5], minor=True)
ax.xaxis.set_major_formatter(ptick.PercentFormatter(xmax=10))
ax.xaxis.set_minor_formatter(ptick.PercentFormatter(xmax=10, decimals=0, symbol=''))
ax.set_yticks(y)
ax.set_yticks([0.5, 1.5, 2.5], minor=True)
ax.yaxis.set_major_formatter(ptick.PercentFormatter(xmax=10, decimals=0, symbol='パーセント'))
ax.yaxis.set_minor_formatter(ptick.PercentFormatter(xmax=10, decimals=2))

# 表示
plt.show()

PercentFormatter【フォーマッタ (パーセント)】のサンプル画像


ScalarFormatter【フォーマッタ (スカラー)】3.1 / 3.3 / 3.4

メモ

  • スカラー フォーマッタ
    • デフォルトのフォーマッタ

構文

class matplotlib.ticker.ScalarFormatter(useOffset=None, useMathText=None, useLocale=None)

useOffset (bool | float)オフセットの使用有無 (省略:True rcParams["axes.formatter.useoffset"])
useMathText (bool)数学フォーマットの使用有無
    True:10N 形式
    False:1eN 形式 (デフォルト:rcParams["axes.formatter.use_mathtext"])
useLocale (bool)ロケール設定の使用有無 (省略:False rcParams["axes.formatter.use_locale"])
属性備考
useLocale
useMathText
useOffset
メソッド備考
format_data(value)
format_data_short(value)
get_offset()
get_useLocale()
get_useMathText()
get_useOffset()
pprint_val() 3.13.3
set_locs(locs)
set_powerlimits(lims)
set_scientific(b)
set_useLocale(val)
set_useMathText(val)
set_useOffset(val)

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [1000, 1003, 1001, 1002]

# グラフ
ax.set_title('ScalarFormatter【フォーマッタ (スカラー)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([0.5, 1.5, 2.5], minor=True)
ax.xaxis.set_major_formatter(ptick.ScalarFormatter())
ax.xaxis.set_minor_formatter(ptick.ScalarFormatter())
ax.set_yticks(y)
ax.set_yticks([1000.5, 1001.5, 1002.5], minor=True)
ax.yaxis.set_major_formatter(ptick.ScalarFormatter(useOffset=1000, useMathText=True))
ax.yaxis.set_minor_formatter(ptick.ScalarFormatter(useOffset=1000, useMathText=True))
ax.yaxis.offsetText.set_color('red')

# 表示
plt.show()

ScalarFormatter【フォーマッタ (スカラー)】のサンプル画像


StrMethodFormatter【フォーマッタ (新フォーマット文字列)】

メモ

  • 新フォーマット文字列指定のフォーマッタ

構文

class matplotlib.ticker.StrMethodFormatter(fmt)

fmtフォーマット文字列 (新スタイル:str.format 形式)
    x:値 (y軸もx使用)
    pos:目盛り位置

import matplotlib.pyplot as plt
import matplotlib.ticker as ptick

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] =\
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, ax = plt.subplots(
    1, 1,
    figsize=(4, 3),
    tight_layout=True,
)
x = [0, 1, 2, 3]
y = [0, 3, 1, 2]

# グラフ
ax.set_title('StrMethodFormatter\n【フォーマッタ (新フォーマット文字列)】')
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticks([0.5, 1.5, 2.5], minor=True)
ax.xaxis.set_major_formatter(ptick.StrMethodFormatter('{x:.1f}'))
ax.xaxis.set_minor_formatter(ptick.StrMethodFormatter('{x}'))
ax.set_yticks(y)
ax.set_yticks([0.5, 1.5, 2.5], minor=True)
ax.yaxis.set_major_formatter(ptick.StrMethodFormatter('{x} ({pos})'))
ax.yaxis.set_minor_formatter(ptick.StrMethodFormatter('{x}'))

# 表示
plt.show()

StrMethodFormatter【フォーマッタ (新フォーマット文字列)】のサンプル画像