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 でエラーバーを設定する方法

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

no image

matplotlib でグラフの中に注釈の文字を書く方法

annotate を使う。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace(-10,10,100) …

no image

matplotlib で、2種類のデータをプロットする方法

下では、凡例(legend)を設定している。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [ …

no image

matplotlib で散布グラフを描く方法

pyplt.scatter を使って散布グラフを描くことができる。 点の色は、点ごとに変えることができる。 下の例では color の配列で点の色を指定している。 例 from matplotlib …

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …