Python Code Notes

Information by python engineer

【matplotlib.pyplot】マーカー・線種・色を設定する方法【Python】

f:id:vigilantPotato:20210517061311p:plain
plotで描画する際にオプションを設定することで、グラフのマーカー・線種・色等、外観を細かく設定するすることができる。モジュールのインポート及び描画するグラフのデータの準備は以下の記事を参照。


【スポンサーリンク】


マーカーの設定

オプションで以下の文字を設定すると、マーカーの種類を設定することができる。

文字列 マーカー 文字列 マーカー
"." point "," pixel
"o" circle "v" triangle down
"^" triangle up "<" triangle left
">" triangle right "1" tri_down
"2" tri_up "3" tri_left
"4" tri_right "s" square
"p" pentagon "*" star
"h" hexagon1 "H" hexagon2
"+" plus "x" x
"D" diamond "d" thin_diamond
"|" vline "_" hline

以下、実際にグラフを描画した例を示す。

fig, ax = plt.subplots()
ax.plot(x, y, ".", label="point")
ax.plot(x, y-1, ",", label="pixel")
ax.plot(x, y-2, "o", label="circle")
ax.plot(x, y-3, "v", label="triangle down")
ax.plot(x, y-4, "^", label="triangle up")
ax.plot(x, y-5, "<", label="triangle left")
ax.plot(x, y-6, ">", label="triangle right")
ax.legend(bbox_to_anchor=(1, 1),)
f:id:vigilantPotato:20190718061232p:plain
fig, ax = plt.subplots()
ax.plot(x, y, "1", label="tri_down")
ax.plot(x, y-1, "2", label="tri_up")
ax.plot(x, y-2, "3", label="tri_left")
ax.plot(x, y-3, "4", label="tri_right")
ax.plot(x, y-4, "s", label="square")
ax.plot(x, y-5, "p", label="pentagon")
ax.plot(x, y-6, "*", label="star")
ax.legend(bbox_to_anchor=(1, 1))
f:id:vigilantPotato:20190718061324p:plain

[↑ 目次へ]


【スポンサーリンク】


線種の設定

マーカーによるプロットではなく、データ間を線で繋げて表示させたい場合は、以下に示す文字列をplotに渡す。

文字列 線種
"-" solid
"--" dashed
"-." dash-dot
":" dotted

以下、実際にグラフを描画した例を示す。

fig, ax = plt.subplots()
ax.plot(x, y, "-", label="solid")
ax.plot(x, y-1, "--", label="dashed")
ax.plot(x, y-2, "-.", label="dash-dot")
ax.plot(x, y-3, ":", label="dotted")
ax.legend(bbox_to_anchor=(1, 1))
f:id:vigilantPotato:20190718061413p:plain

[↑ 目次へ]


【スポンサーリンク】


色の設定

マーカーや線の色を設定するには、以下の文字を使用する。

文字列
"b" blue
"g" green
"r" red
"c" cyan
"m" magenta
"y" yellow
"k" black
"w" white
fig, ax = plt.subplots()
ax.plot(x, y, "b", label="blue")
ax.plot(x, y-1, "g", label="green")
ax.plot(x, y-2, "r", label="red")
ax.plot(x, y-3, "c", label="cyan")
ax.plot(x, y-4, "m", label="magenta")
ax.plot(x, y-5, "y", label="yellow")
ax.plot(x, y-6, "k", label="black")
ax.plot(x, y-7, "w", label="white")
ax.legend(bbox_to_anchor=(1, 1))
f:id:vigilantPotato:20190718061455p:plain

[↑ 目次へ]


組み合わせ

マーカー、線種、色を一括して設定することができる。以下、線種を点線、マーカーを〇、青色でプロットする例を示す。

fig, ax = plt.subplots()
ax.plot(x, y, "--ob")
f:id:vigilantPotato:20190716235320p:plain

[↑ 目次へ]