【matplotlib】離散データを表示するステムプロット(stem plot)[Python]

  • URLをコピーしました!

matplotlib

前回、ヒストグラム用のガウス分布(正規分布)に従うランダムな値のリストを複数生成する方法を紹介しました。

今回はmatplotlibで離散データを表示するステムプロット(stem plot)を紹介します。

それでは始めていきましょう。

ステムプロットの基本的な使い方

ステムプロットを作成するには散布図の様にXの値のリストとYの値のリストを準備します。

そして「plt.stem(X値のリスト, Y値のリスト)」として用います。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y)

plt.show()

実行結果

また「bottom=値」のオプション引数を使うことでベースラインの高さを変えることができます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, bottom=50)

plt.show()

実行結果

また「orientation=”horizontal”」のオプション引数を追加することで、横向きにすることができます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, orientation="horizontal")

plt.show()

実行結果

ステム(幹)の線の変更

ステム(幹)の線は4種類準備されており、「linefmt」のオプション引数にそれぞれ「”-“」「”–“」「”-.”」「”:”」で指定することができます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="-")

plt.show()

実行結果
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="--")

plt.show()

実行結果
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="-.")

plt.show()

実行結果
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt=":")

plt.show()

実行結果

ステム(幹)の色を変えるには「linefmt=”r:”」のように色の一文字表記を線種の前に付けます。

一文字表記で指定できる色はこちらのmatplotlibの公式ページで確認できます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="r:")

plt.show()

実行結果

ちなみに一文字表記ではなく、全部書いてしまうと(つまりrではなくredと書いてしまう)エラーとなります。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="red:")

plt.show()

実行結果
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 12
      9 fig = plt.figure()
     10 plt.clf()
---> 12 plt.stem(x, y, linefmt="red:")
     14 plt.show()

(中略)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/axes/_base.py:197, in _process_plot_format(fmt, ambiguous_fmt_datakey)
    195         i += 2
    196     else:
--> 197         raise ValueError(
    198             errfmt.format(fmt, f"unrecognized character {c!r}"))
    200 if linestyle is None and marker is None:
    201     linestyle = mpl.rcParams['lines.linestyle']

ValueError: 'red:' is not a valid format string (unrecognized character 'e')

またmatplotlibの基本色(tab:blueやtab:orange)などは色の一文字表記の代わりに、それぞれ「C0」、「C1」を線種の前に付けると使用できます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, linefmt="C1:")

plt.show()

実行結果

ベースラインの線の変更

ベースラインの線を変更するには「basefmt」をオプション引数に追加します。

選択できる線の種類はステム(幹)の線と同じ4種類です。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, basefmt="--")

plt.show()

実行結果

色を変える場合はステム(幹)の色を変えるのと同様に線種の前に色の一文字表記か「C0」の表記を追加します。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, basefmt="k--")

plt.show()

実行結果
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, basefmt="C1-.")

plt.show()

実行結果

マーカーの変更

ステム(幹)の先にあるマーカーを変えるには「markerfmt」をオプション引数に追加します。

選択できるマーカーはこちらのmatplotlibnの公式ページで確認できます。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, markerfmt="*")

plt.show()

実行結果

色を変える場合はステム(幹)の色を変えるのと同様にマーカー種の前に色の一文字表記か「C0」の表記を追加します。

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, markerfmt="rX")

plt.show()

実行結果
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)

x = np.arange(25)
y = [rng.integers(100) for _ in x]

fig = plt.figure()
plt.clf()

plt.stem(x, y, markerfmt="C1X")

plt.show()

実行結果

次回はNumPyでリスト内の整数をカウント(ただし負の数は除く)するbincountを紹介します。

ではでは今回はこんな感じで。

よかったらシェアしてね!
  • URLをコピーしました!

コメント

コメントする