step【折れ線グラフ (ステップ)】

メモ ( 基本 データの指定方法 関連設定 ) 構文 設定・グラフパーツ

メモ

stepのサンプル

基本

データの指定方法

  • 単一データ指定 (同一座標系に複数可)
  • 複数データの一括指定
  • データ引数にラベル名を指定、data (ラベル名指定データ)にラベル名とデータを指定
  • データを文字列指定 (数値の指定なし)

関連設定

構文

matplotlib.pyplot.step(
    x (x軸データ)※data,
    y (y軸データ)※data,
    *args (x軸データ・y軸データ・簡易スタイル),
    where (ステップ位置)='pre',
    data (ラベル名指定データ)=None,
    **kwargs)
matplotlib.axes.Axes.step(同上)
戻り値Line2D【線分】 のリスト

x (配列形式)※datax軸のデータ
y (配列形式)※datay軸のデータ
args (可変長位置引数)下記が有効 (位置引数としてのみ有効)
    x, y[, fmt] の繰り返し (複数の折れ線作成)
        x (配列形式) 位置引数のみ:x軸のデータ
        y (配列形式) 位置引数のみ:y軸のデータ
        fmt (str) 位置引数のみ:簡易スタイル (フォーマット文字列 の色指定のみを推奨:その他はLine2D【線分】のプロパティで指定)
where (キーワード引数)ステップ位置 ('pre':左側 / 'post':右側 / 'mid':中央)
data (キーワード引数)ラベル名指定データ (dict【辞書型】・pandas.DataFrame・構造化numpy配列 等)
    (※data:ラベル名指定可・詳細は、データの指定方法 参照・下記が対象)
    ・x (x軸データ)
    ・y (y軸データ)
kwargs (可変長キーワード引数)Line2D【線分】のプロパティ

フォーマット文字列

'[簡易marker (マーカースタイル)][簡易line (線種)][簡易color (色)]'

※:順不同

簡易マーカースタイル

簡易markerマーカースタイル
. (ドット)pointポイント
point marker
, (カンマ)pixelピクセル
pixel marker
o (オー)circle
circle marker
v (ブイ)triangle_down三角形 (下向き)
triangle_down marker
^ (山型アクセント記号)triangle_up三角形 (上向き)
triangle_up marker
<triangle_left三角形 (左向き)
triangle_left marker
>triangle_right三角形 (右向き)
triangle_right marker
1tri_down三芒星 (下向き)
tri_down marker
2tri_up三芒星 (上向き)
tri_up marker
3tri_left三芒星 (左向き)
tri_left marker
4tri_right三芒星 (右向き)
tri_right marker
8 3.4octagon角形
octagon
ssquare四角形
square marker
ppentagon五角形
pentagon marker
P 3.4plus (filled)プラス記号 (塗りつぶし)
plus (filled) marker
*star星印
star marker
hhexagon1六角形 (1)
hexagon1 marker
Hhexagon2六角形 (2)
hexagon2 marker
+plusプラス記号
plus marker
xxバツ印
x marker
X 3.4x (filled)バツ印 (塗りつぶし)
x (filled) marker
Ddiamondダイヤモンド
diamond marker
dthin_diamondダイヤモンド (縦長)
thin_diamond marker
|vline垂直線
vline marker
_ (アンダースコア)hline水平線
hline marker

簡易線種

簡易line線種
-solid実線
--dashed破線
dashed line style
-.dashdot一点鎖線
dash-dot line style
:dotted点線
dotted line style

簡易色

color備考
bblue詳細は 色名の省略形 (1文字) 参照
ggreen
rred
ccyan
mmagenta
yyellow
kkey (black)
wwhite
CnCNカラー (n:色番号)
詳細は CN カラー 参照
簡易marker (マーカースタイル)の数字指定との組合せに注意
その他色の単独指定の場合、全ての 色定義 の指定が可能

Line2D【線分】のプロパティ

プロパティデフォルトget_*set_*備考
agg_filtercallableArtistArtistagg フィルター
alphaスカラー
None
ArtistArtistアルファ値 (0:透明 ~ 1:不透明)
(線とマーカー共有)
animatedboolArtistArtistアニメーション使用の有無
antialiased
aa 3.1
bool

アンチエイリアス処理の有無
clip_boxBboxArtistArtist
clip_onboolArtistArtistクリッピングの有無 (True:範囲内のみ描画 / False:範囲外にも描画可)
clip_pathPatch
(Path, Transform)
None
ArtistArtist
color
c 3.1
color【色定義】

線の色
contains 3.3callableget_pickradiusset_pickradiusピッカーイベント (参照:picker【ピッカーイベント】)
dash_capstyleCapStyle3.4
str
'projecting'実線以外の線端スタイル (実線は solid_capstyle 参照)
スタイル備考
'butt'
_enums.CapStyle.butt
butt
'projecting'
_enums.CapStyle.projecting
projecting
'round'
_enums.CapStyle.round
round
dash_joinstyleJoinStyle3.4
str
'round'実線以外の結合スタイル (実線は solid_joinstyle 参照)
スタイル備考
'bevel'
_enums.JoinStyle.bevel
bevel
'miter'
_enums.JoinStyle.miter
miter
'round'
_enums.JoinStyle.round
round
dashessequence of floats (on/off ink in points)
(None, None)
get_linestyle
data(2, N) array
two 1D arrays
drawstyle
ds 3.1
str'default'

線の接続スタイル
スタイル備考
'default'default直線
'steps'steps始点でステップ (互換用)
'steps-pre'steps-pre始点でステップ
'steps-mid'steps-mid中間でステップ
'steps-post'steps-post終点でステップ
figureFigureArtistArtist所属フィギュア
fillstylestrマーカー塗りつぶしスタイル (参照:markerfacecolormarkerfacecoloralt)
スタイル備考
'bottom'bottom下半分塗りつぶし
'full'full全体塗りつぶし
'left'left左半分塗りつぶし
'none'none塗りつぶしなし
'right'right右半分塗りつぶし
'top'top上半分塗りつぶし
gapcolor 3.6color【色定義】
None
破線スタイルのギャップ色
gidstrArtistArtistグループId (識別子として利用可)
in_layoutboolArtistArtistレイアウト計算に含めるか否か
labelobjectArtistArtist凡例ラベル (アンダースコアで始まるラベルは対象外)
linestyle
ls 3.1
str
(offset, on-off-seq)


線種
スタイル備考
'-' (ダッシュ)
'solid'
solid実線
solid line
'--' (ダブルダッシュ)
'dashed'
dashed破線
dashed line
'-.'
'dashdot'
dashdot一点鎖線
dash-dotted line
':' (コロン)
'dotted'
dotted点線
dotted line
'None'
' ' (半角スペース)
'' (空文字列)
線なし
(offset, on-off-seq)
(offset, on-off-seq)
ユーザ定義
offset:開始位置オフセット
on-off-seq:線のありなしの対
例:二点鎖線
(0, (6.5, 1.5, 1, 1.5, 1, 1.5))
'steps~' 3.13.3drawstyle の 'steps' との組合せ
linewidth
lw 3.1
float

線の幅
markerstr
Path
MarkerStyle
マーカースタイル (詳細は、マーカー定義 参照)
markeredgecolor
mec 3.1
color【色定義】

マーカー外枠の色
markeredgewidth
mew 3.1
float

マーカー外枠の幅
markerfacecolor
mfc 3.1
color【色定義】

マーカーの塗りつぶし色 (参考:fillstyle)
markerfacecoloralt
mfcalt 3.1
color【色定義】

fillstyle【マーカー塗りつぶしスタイル】指定の残り部分の塗りつぶし色
markersize
ms 3.1
float

マーカーのサイズ (ポイント単位)
markevery各種 (マーカー頻度 参照)マーカー頻度 (詳細 参照)
path_effectsAbstractPathEffectArtistArtist
pickercallable[[Artist, Event], tuple[bool, dict]]
float 3.3
Artistcallable:ピッカーイベント
float:ピッカー有効半径 (参照:contains【ピッカーイベント】3.3 )
pickradiusfloatピッカー有効半径
rasterizedboolArtistArtistラスタライズ(ビットマップ) 描画の強制有無
sketch_params(scale: float, length: float, randomness: float)ArtistArtist
snapbool
None
ArtistArtistスナップ (ピクセル位置補正) の有無
(Agg・MacOSX バックエンドのみサポート)
solid_capstyleCapStyle3.4
str
'projecting'実線の線端スタイル (実線以外は dash_capstyle 参照)
スタイル備考
'butt'
_enums.CapStyle.butt
butt
'projecting'
_enums.CapStyle.projecting
projecting
'round'
_enums.CapStyle.round
round
solid_joinstyleJoinStyle3.4
str
'round'実線の結合スタイル (実線以外は dash_joinstyle 参照)
スタイル備考
'bevel'
_enums.JoinStyle.bevel
bevel
'miter'
_enums.JoinStyle.miter
miter
'round'
_enums.JoinStyle.round
round
transformmatplotlib.transforms.TransformArtist座標変換
urlstrArtistArtistハイパーリンクURL (SVG のみ対応)
visibleboolArtistArtist表示有無
xdata1D arrayx軸データ
ydata1D arrayy軸データ
zorderfloatArtistArtistZオーダー (大きい方が手前)

設定・グラフパーツ

設定・グラフパーツ の抜粋〔

pyplot.axes.Axes.その他
タイトルsuptitle【タイトル設定 (全体)】
title【タイトル設定】
set_title【タイトル設定】
get_title【タイトル取得】
Axes3D.set_title【タイトル設定】
figure.Figure.suptitle【タイトル設定 (全体)】
figure.SubFigure.suptitle
凡例legend【凡例 設定】3.5 / 3.73.8
quiverkey【ベクトルの凡例 追加】
figure.Figure.legend【凡例 設定 (全体)】3.73.8
PathCollection.legend_elements【凡例項目作成】
get_legend【凡例 取得】
get_legend_handles_labels【凡例 取得 (ハンドル・ラベル)】
軸ラベルxlabel【x軸ラベル 設定】3.3
ylabel【y軸ラベル 設定】3.3
set_xlabel【x軸ラベル 設定】3.3
set_ylabel【y軸ラベル 設定】3.3
Axes3D.set_zlabel【z軸ラベル 設定】

get_xlabel【x軸ラベル 取得】
get_ylabel【y軸ラベル 取得】
Axes3D.get_zlabel【z軸ラベル 取得】
figure.Figure.supxlabel【x軸ラベル(全体) 設定】3.43.6
figure.Figure.supylabel【y軸ラベル(全体) 設定】3.43.6
figure.SubFigure.supxlabel
figure.SubFigure.supylabel
clabel【等高線ラベル 設定】
bar_label【バーラベル】3.43.7
グリッド線grid【グリッド線 設定】3.5grid【グリッド線 設定】3.5
get_xgridlines【x軸グリッド線 取得】
get_ygridlines【y軸グリッド線 取得】
(Axes3D.grid【3Dグリッド線 取得・設定】)
Axes3D.get_zgridlines【z軸グリッド線 取得】
axis.Axis.grid【軸グリッド線 設定】3.5
axis.Axis.get_gridlines【軸グリッド線 取得】
triplot【三角形分割】
rgrids【放射状グリッド線 取得・設定】
thetagrid【放射状シータグリッド線 取得・設定】
projections.polar.PolarAxes.set_rgrids【放射状グリッド線 設定】
projections.polar.PolarAxes.set_thetagrid【放射状シータグリッド線 設定】
表示範囲xlim【x軸表示範囲 取得・設定】
ylim【y軸表示範囲 取得・設定】
axis【表示範囲等 取得・設定】3.1~3.4 / 3.7
set_xlim【x軸表示範囲 設定】3.1 / 3.6
set_ylim【y軸表示範囲 設定】3.1 / 3.6
get_xlim【x軸表示範囲 取得】
get_ylim【y軸表示範囲 取得】
axis【表示範囲等 取得・設定】3.1~3.4 / 3.7
projections.polar.PolarAxes.set_rlim【極座標範囲 設定】
Axes3D.set_xlim【3D x軸表示範囲 設定】
Axes3D.set_xlim3d【3D x軸表示範囲 設定】
Axes3D.set_ylim【3D y軸表示範囲 設定】
Axes3D.set_ylim3d【3D y軸表示範囲 設定】
Axes3D.set_zlim【3D z軸表示範囲 設定】
Axes3D.set_zlim3d【3D z軸表示範囲 設定】
Axes3D.get_xlim3d【3D x軸表示範囲 取得】
Axes3D.get_ylim3d【3D y軸表示範囲 取得】
Axes3D.get_zlim3d【3D z軸表示範囲 取得】
データ上限・下限set_xbound【x軸データ上限・下限 設定】
set_ybound【y軸データ上限・下限 設定】
get_xbound【x軸データ上限・下限 取得】
get_ybound【y軸データ上限・下限 取得】
projections.polar.PolarAxes.set_rmax【極座標上限 設定】
projections.polar.PolarAxes.set_rmin【極座標下限 設定】
目盛りxticks【x軸目盛り (ラベル) 取得・設定】3.6
yticks【y軸目盛り (ラベル) 取得・設定】3.6
locator_params【目盛り制御】
minorticks_on
minorticks_off
ticklabel_format【目盛りフォーマット設定 (指数形式)】
tick_params【目盛り・グリッド線 外観変更】
set_xticks【x軸目盛り 設定】3.2 / 3.5
set_yticks【y軸目盛り 設定】3.2 / 3.5
Axes3D.set_zticks【z軸目盛り 設定】

set_xticklabels【x軸目盛りラベル 設定】3.3 / 3.5
set_yticklabels【y軸目盛りラベル 設定】3.3 / 3.5
Axes3D.set_zticklabels【z軸目盛りラベル 設定】

get_xticks【x軸目盛り 取得】3.2
get_yticks【y軸目盛り 取得】3.2
Axes3D.get_zticks【z軸目盛り 取得】

get_xticklabels【x軸目盛りラベル 取得】3.3 / 3.6
get_yticklabels【y軸目盛りラベル 取得】3.3 / 3.6
Axes3D.get_zticklabels【z軸目盛りラベル 取得】

get_xmajorticklabels【x軸目盛りラベル 取得 (主)】3.3 / 3.6
get_ymajorticklabels【y軸目盛りラベル 取得 (主)】3.3 / 3.6
get_xminorticklabels【x軸目盛りラベル 取得 (補助)】3.3 / 3.6
get_yminorticklabels【y軸目盛りラベル 取得 (補助)】3.3 / 3.6
get_xticklines
get_yticklines
xaxis_date
yaxis_date
minorticks_on
minorticks_off
ticklabel_format【目盛りフォーマット設定 (指数形式)】
tick_params【目盛り・グリッド線 外観変更】
locator_params【目盛り制御】
fmt_xdata
fmt_ydata
ticker【目盛りモジュール】
axis.Axis.set_ticks【目盛り 設定】
axis.Axis.set_ticklabels【目盛りラベル 設定】
axis.Axis.set_tick_params【目盛り・グリッド線 外観変更】
axis.XAxis.set_ticks_position【x軸目盛り位置 設定】
axis.YAxis.set_ticks_position【y軸目盛り位置 設定】
Axes3D.locator_params【目盛り制御】

axis.Axis.set_major_formatter【フォーマッタ設定 (主)】
axis.Axis.set_minor_formatter【フォーマッタ設定 (補助)】
axis.Axis.set_major_locator【ロケータ設定 (主)】
axis.Axis.set_minor_locator【ロケータ設定 (補助)】
axis.Axis.get_ticklabels【目盛りラベル取得】
axis.Axis.get_majorticklabels【目盛りラベル取得 (主)】
axis.Axis.get_minorticklabels【目盛りラベル取得 (補助)】
axis.Axis.get_major_formatter【フォーマッタ取得 (主)】
axis.Axis.get_minor_formatter【フォーマッタ取得 (補助)】
axis.Axis.get_ticklocs
axis.Axis.get_major_locator【ロケータ取得 (主)】
axis.Axis.get_minor_locator【ロケータ取得 (補助)】
figure.Figure.autofmt_xdate【x軸日付目盛りラベル回転】
axis.Axis.get_tick_params【目盛り・グリッド線 外観取得】3.7
直線
範囲
axhline【水平線 (単一)】
axvline【垂直線 (単一)】
hlines【水平線 (複数)】3.3
vlines【垂直線 (複数)】3.3
axhspan【水平範囲】
axvspan【垂直範囲】
axline【直線】3.3

基本

import matplotlib.pyplot as plt

# 初期設定
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(
    figsize=(5, 4),
    tight_layout=True,
)

# グラフ
x = ['X_1', 'X_2', 'X_3', 'X_4', 'X_5']
ax.step(
    x,
    [1, 2, 3, 4, 5],
    'red',
    where='pre',
    label="where='pre' (左側)",
    linewidth=3,
    marker='o',
    markersize=8,
)
ax.step(
    x,
    [3, 4, 5, 6, 7],
    'green',
    where='mid',
    label="where='mid' (中央)",
    linewidth=3,
    marker='o',
    markersize=8,
)
ax.step(
    x,
    [5, 6, 7, 8, 9],
    'blue',
    where='post',
    label="where='post' (右側)",
    linewidth=3,
    marker='o',
    markersize=8,
)
ax.legend()

# 表示
plt.show()

stepのサンプル画像


データの指定方法

import matplotlib.pyplot as plt

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

# 単一データ (複数回)
axs[0, 0].set_title('単一データ (複数回)')
axs[0, 0].step(
    [1, 2, 3, 4, 5],
    [1, 2, 3, 4, 5],
    'red',
)
axs[0, 0].step(
    [1, 2, 3, 4, 5],
    [3, 4, 5, 6, 7],
    'green',
)
axs[0, 0].step(
    [1, 2, 3, 4, 5],
    [5, 6, 7, 8, 9],
    'blue',
)

# 複数データ (一括)
axs[0, 1].set_title('複数データ (一括)')
axs[0, 1].step(
    [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], 'red',
    [1, 2, 3, 4, 5], [3, 4, 5, 6, 7], 'green',
    [1, 2, 3, 4, 5], [5, 6, 7, 8, 9], 'blue',
)

# data 指定
axs[0, 2].set_title('data 指定')
axs[0, 2].step(
    'data_x',
    'data_y',
    'red',
    data={
        'data_x': [1, 2, 3, 4, 5],
        'data_y': [1, 2, 3, 4, 5],
    },
)
axs[0, 2].step(
    'data_x',
    'data_y',
    'green',
    data={
        'data_x': [1, 2, 3, 4, 5],
        'data_y': [3, 4, 5, 6, 7],
    },
)
axs[0, 2].step(
    'data_x',
    'data_y',
    'blue',
    data={
        'data_x': [1, 2, 3, 4, 5],
        'data_y': [5, 6, 7, 8, 9],
    },
)

# x軸データ文字列 指定
axs[1, 0].set_title('x軸データ文字列 指定')
axs[1, 0].step(
    ['X_1', 'X_2', 'X_3', 'X_4', 'X_5'],
    [1, 2, 3, 4, 5],
)

# y軸データ文字列 指定
axs[1, 1].set_title('y軸データ文字列 指定')
axs[1, 1].step(
    [1, 2, 3, 4, 5],
    ['Y_1', 'Y_2', 'Y_3', 'Y_4', 'Y_5'],
)

# xy軸データ文字列 指定
axs[1, 2].set_title('xy軸データ文字列 指定')
axs[1, 2].step(
    ['X_1', 'X_2', 'X_3', 'X_4', 'X_5'],
    ['Y_1', 'Y_2', 'Y_3', 'Y_4', 'Y_5'],
)

# 表示
plt.show()

stepのサンプル画像


設定・グラフパーツ

import matplotlib.pyplot as plt

# 初期設定
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = \
    ['Yu Gothic', 'Hiragino Maru Gothic Pro', 'Noto Sans CJK JP']
fig, axs = plt.subplots(
    1, 2,
    figsize=(6, 3),
    tight_layout=True,
)
# タイトル (全体)
plt.suptitle('タイトル (全体)')

# グラフ (左)
axs[0].step(
    [1, 2, 3, 4, 5],
    [1, 2, 3, 4, 5],
    label='Data 1',
)
axs[0].step(
    [1, 2, 3, 4, 5],
    [3, 4, 5, 6, 7],
    label='Data 2',
)
axs[0].step(
    [1, 2, 3, 4, 5],
    [5, 6, 7, 8, 9],
    label='Data 3',
)
# タイトル
axs[0].set_title('タイトル (左)')
# 凡例
axs[0].legend(title='凡例')
# 軸ラベル
axs[0].set_xlabel('x軸ラベル')
axs[0].set_ylabel('y軸ラベル')
# グリッド
axs[0].grid(True)
# 表示範囲
axs[0].set_xlim(0, 6)
axs[0].set_ylim(0, 15)
# 目盛り
axs[0].set_xticks([0, 1, 2, 3, 4, 5, 6])
axs[0].set_yticks([0, 2, 4, 6, 8, 10])
# 補助線 追加
axs[0].axline((1, 3), (5, 7), alpha=0.5)

# グラフ (右)
axs[1].step(
    [1, 2, 3, 4, 5], [1, 2, 3, 4, 5],
    [1, 2, 3, 4, 5], [3, 4, 5, 6, 7],
    [1, 2, 3, 4, 5], [5, 6, 7, 8, 9],
)
# タイトル
axs[1].set_title('タイトル (右)')
# データ上限・下限
axs[1].set_xbound(2, 4)
axs[1].set_ybound(2, 8)
# 目盛り
axs[1].set_xticks([2, 3, 4])
axs[1].set_xticklabels(['X (2)', 'X (3)', 'X (4)'])
axs[1].set_yticks([2, 4, 6, 8])
axs[1].set_yticklabels(['Y (2)', 'Y (4)', 'Y (6)', 'Y (8)'])
# 水平線・垂直線 追加
axs[1].axhline(1.5, linestyle='--')
axs[1].axhline(7.5, linestyle='--')
axs[1].axvline(2.5, linestyle='--')
axs[1].axvline(4.5, linestyle='--')
# 範囲 追加
axs[1].axhspan(
    2.5, 6.5,
    alpha=0.8,
    facecolor='lightgreen',
    edgecolor='blue',
)
axs[1].axvspan(
    2.75, 4.25,
    alpha=0.8,
    facecolor='lightgreen',
    edgecolor='blue',
)

# 表示
plt.show()

stepのサンプル画像