matplotlib

matplotlib のプロットスクリプト内で計算する方法

投稿日:

gnuplot の場合

gnuplot では、plot “data.txt” u 1:($2*2)などと書くと、

数値を加工(データ値を使った計算)を行った結果をグラフ化することができる。

matplotlib の場合

いっぽう、matplotlib では map を使ってデータ(リスト)を操作することにより、データを加工することができる。

各データに対して、myfunction という関数を作用させたものをプロットする。

# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-2,5,50)
y = x

def myfunction(x):
    return x*x - 2 * x + 1

y2 = map(myfunction, y)

plt.rcParams['font.family'] = 'Hiragino Sans'
plt.plot(x, list(y2), label='関数', marker='o')
plt.xlabel('年')
plt.ylabel('トン')
plt.legend()
plt.show()

結果

-matplotlib
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、グラフ内に注釈ラベルをつける方法

annotate を使う 例 import numpy as np import matplotlib.pyplot as plt ax = plt.figure().add_subplot() th …

no image

matplotlib の ax でx軸、y軸の端の値を設定する

axes には、set_xlim として定義する。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace( …

no image

matplotlib で、凡例を表示する方法

データに label を関連付け、plt.legend() で凡例を表示する。 例 from matplotlib import pyplot as plt data_x = [25,26,2 …

no image

matplotlib で、x,y の軸上の数値を表示する方法

ax の xtics と ytics を使って表示する。 詳細は次のドキュメントを参照。 https://matplotlib.org/stable/gallery/lines_bars_and_ma …

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …